Using Dispatcher
Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
switch (request.getPath()) {
case "/get":
return new MockResponse().setResponseCode(200).setBody("test");
}
return new MockResponse().setResponseCode(404);
}
};
mockBackEnd.setDispatcher(dispatcher);
Above can be written inside your test method. You can have a bunch of URLs conditions there.
The mockwebserver can be started once like:
public static MockWebServer mockBackEnd;
@BeforeAll
static void setUp() throws IOException {
mockBackEnd = new MockWebServer();
mockBackEnd.start();
}
@AfterAll
static void tearDown() throws IOException {
mockBackEnd.shutdown();
}
Use @DynamicPropertySource
to change any property with mockserver host/port.