I would like to create a unit test using a mock web server. Is there a web server written in Java which can be easily started and stopped from a JUnit test case?
I would like to create a unit test using a mock web server. Is there a web server written in Java which can be easily started and stopped from a JUnit test case?
Are you trying to use a mock or an embedded web server?
For a mock web server, try using Mockito, or something similar, and just mock the HttpServletRequest and HttpServletResponse objects like:
MyServlet servlet = new MyServlet();
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
StringWriter out = new StringWriter();
PrintWriter printOut = new PrintWriter(out);
when(mockResponse.getWriter()).thenReturn(printOut);
servlet.doGet(mockRequest, mockResponse);
verify(mockResponse).setStatus(200);
assertEquals("my content", out.toString());
For an embedded web server, you could use Jetty, which you can use in tests.
Try Simple(Maven) its very easy to embed in a unit test. Take the RoundTripTest and examples such as the PostTest written with Simple. Provides an example of how to embed the server into your test case.
Also Simple is much lighter and faster than Jetty, with no dependencies. So you won't have to add several jar files onto your classpath. Nor will you have to be concerned with WEB-INF/web.xml or any other artifacts.
If you are using apache HttpClient, This will be a good alternative. HttpClientMock
HttpClientMock httpClientMock = new httpClientMock()
HttpClientMock("http://example.com:8080");
httpClientMock.onGet("/login?user=john").doReturnJSON("{permission:1}");
Basically, you then make requests on your mock object and then can do some verifies on it httpClientMock.verify().get("http://localhost/login").withParameter("user","john").called()
I recommend Javalin. It's an excellent tool for mocking the real service as it allows for state assertions in your tests (server side assertions).
Wiremock can be used as well. But it leads to hard to maintain behavioral tests (verify that client calls are as expected).
In the interest of completeness there is also wrapping jetty with camel to make it slightly more user friendly.
make your test class extend CamelTestSupport then define a route ex:
@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {
@Override
public void configure() {
from("jetty:http://localhost:" + portToUse).process(
new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// Get the request information.
requestReceivedByServer = (String) exchange.getIn().getHeader(Exchange.HTTP_PATH);
// For testing empty response
exchange.getOut().setBody("your response");
....
example maven dependencies to get it:
<dependency> <!-- used at runtime, by camel in the tests -->
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>2.12.1</version>
<scope>test</scope>
</dependency>