0

I am trying to start a mock a server from Java and keep it running to receive incoming requests from other sources (Postman, CURL, etc).

I have tried the Junit approach, but when the unit tests finishes, the server is shutdown. On the other hand, running the standalone version http://www.mock-server.com/mock_server/running_mock_server.html#running_from_command_line

keeps the mock server running.

I would like to achieve the same thing, but from the Java code.

The question is, how may I make it run and stay running?

Thanks

2 Answers2

0

So you need an HTTP server for non-testing purposes? I'd try with Spring, something like:

@RestController
public class CatchAllController {

    @RequestMapping("/**")
    public String catchAll(HttpServletRequest request) {
        return request.getRequestURI();
    }
}
Qualsiasi
  • 51
  • 2
  • 6
0

There is an example on that same page (paragraph "Client API - starting and stopping"). This code works for me:


import static org.mockserver.integration.ClientAndServer.startClientAndServer;
import org.mockserver.integration.ClientAndServer;

public class Checker {

  public static void main(String[] args) {

    ClientAndServer mockServer = startClientAndServer(1080);

  }
}

You have to call

mockServer.stop();

later to stop it.

You will need the following maven dependency:

<!-- mockserver -->
<dependency>
     <groupId>org.mock-server</groupId>
     <artifactId>mockserver-netty</artifactId>
     <version>5.5.1</version>
</dependency>
Jens Dibbern
  • 1,434
  • 2
  • 13
  • 20