A scriptable web server for testing HTTP clients
This project is a fork of the project okhttp-mockwebserver (created by Square). This evolution targets any Java project with Java >= 8 and with Junit >= 5. You can use the Junit 5 extension, MockWebServerExtension
. This project exists because the initial one won’t upgraded to Java 8 and Junit 5 immediately.
The original version has also several warning on compilation because of the classes used to generate certificate. This fork fix them
This project is managed by Dev-Mind but you can send push request or open issues
This library makes it easy to test that your app Does The Right Thing when it makes HTTP and HTTPS calls. It lets you specify which responses to return and then verify that requests were made as expected.
Because it exercises your full HTTP stack, you can be confident that you’re testing everything. You can even copy & paste HTTP responses from your real web server to create representative test cases. Or test that your code survives in awkward-to-reproduce situations like 500 errors or slow-loading responses.
Use MockWebServerExtension the same way that you use other frameworks extensions in Junit 5 tests.
For example if you want to test a Spring service in a WebFlux app you could use
@ExtendWith(MockWebServerExtension.class)
class MySpringWebfluxServiceTest {
private MockWebServer server;
private WebClient webClient;
private MySpringWebfluxService service;
@BeforeEach
public void setup(MockWebServer server) {
this.webClient = WebClient.create(server.url("/").toString());
this.service = new MySpringWebfluxService(webClient);
this.server = server;
}
@Test
public void mytest() throws Exception {
prepareResponse(response -> response
.setHeader("Content-Type", "application/json")
.setBody( "{\n" +
" \"error_message\" : \"The provided API key is invalid.\",\n" +
" \"predictions\" : [],\n" +
" \"status\" : \"REQUEST_DENIED\"\n" +
"}"));
StepVerifier.create(service.myMethod())
.expectComplete()
.verify(Duration.ofSeconds(3));
}
private void prepareResponse(Consumer consumer) {
MockResponse response = new MockResponse();
consumer.accept(response);
this.server.enqueue(response);
}
}
The server is automatically launched and stopped before and after each tests. If you want to manage the lifecycle by yourself you can use MockSimpleWebServerExtension
For example
@ExtendWith(MockSimpleWebServerExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class MySpringWebfluxServiceTest {
private MockWebServer server;
private WebClient webClient;
private MySpringWebfluxService service;
@BeforeAll
public void init(MockWebServer server) throws IOException {
server.start();
this.server = server;
}
@AfterAll
public void tearDown() throws IOException {
server.shutdown();
}
@BeforeEach
public void setup(MockWebServer server) {
this.webClient = WebClient.create(server.url("/").toString());
this.service = new MySpringWebfluxService(webClient);
}
@Test
public void mytest() throws Exception {
prepareResponse(response -> response
.setHeader("Content-Type", "application/json")
.setBody( "{\n" +
" \"error_message\" : \"The provided API key is invalid.\",\n" +
" \"predictions\" : [],\n" +
" \"status\" : \"REQUEST_DENIED\"\n" +
"}"));
StepVerifier.create(service.myMethod())
.expectComplete()
.verify(Duration.ofSeconds(3));
}
private void prepareResponse(Consumer consumer) {
MockResponse response = new MockResponse();
consumer.accept(response);
this.server.enqueue(response);
}
}
Get MockWebServer via Maven:
<dependency>
<groupId>fr.dev-mind</groupId>
<artifactId>mockwebserver</artifactId>
<version>(insert latest version)</version>
<scope>test</scope>
</dependency>
or via Gradle
testCompile 'fr.dev-mind.mockwebserver:(insert latest version)'
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.