Spring Boot End-to-end Testing Using Mock Server

testImplementation("org.mock-server:mockserver-netty:5.13.0")
testImplementation("org.mock-server:mockserver-client-java:5.13.0")
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>5.13.0</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>5.13.0</version>
</dependency>
@RestController
public class
DemoController {
@Value("${api.url.helloWorld}")
private String helloWorldURL;

@Autowired
private
RestTemplate restTemplate;

@GetMapping("/demo")
public ResponseEntity<String> demo(HttpServletRequest httpServletRequest) {
RequestEntity<String> requestEntity = new HttpEntityBuilder<String>()
.withURL(helloWorldURL)
.withHttpMethod(HttpMethod.GET)
.build();
String responseBody = restTemplate.exchange(requestEntity, String.class).getBody();
return new ResponseEntity<>(responseBody, HttpStatus.OK);
}
}
api.url.helloWorld=http://localhost:8081/api/helloWorld
public class MockServerConfig {
private static final Integer MOCK_SERVER_PORT = 1080;
private final ClientAndServer clientAndServer;
private final MockServerClient mockServerClient = new MockServerClient("localhost", MOCK_SERVER_PORT);

MockServerConfig() {
this.clientAndServer = ClientAndServer.startClientAndServer(MOCK_SERVER_PORT);
}

public void registerHelloWorldEndpoint() {
mockServerClient
.when(
request()
.withMethod("GET")
.withPath("/api/helloWorld"),
exactly(1)
)
.respond(
response()
.withStatusCode(200)
.withBody("Mocked Response!!!!!!")
.withDelay(TimeUnit.SECONDS, 1)
);
}

public void verifyHelloWorldEndpoint() {
mockServerClient.verify(
request()
.withMethod("GET")
.withPath("/api/helloWorld"),
VerificationTimes.exactly(1)
);
}

public void stopServer() {
clientAndServer.stop();
}
}
public void verifyMockEndpoint() {
mockServerClient.verify(
request()
.withMethod("GET")
.withPath("/api/helloWorld"),
VerificationTimes.exactly(1)
);
}
@SpringBootTest
@AutoConfigureMockMvc
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DemoControllerE2ETest {
@Autowired
MockMvc mockMvc;
private MockServerConfig mockServerConfig; @BeforeAll
public void startServer() {
mockServerConfig = new MockServerConfig();
mockServerConfig.registerHelloWorldEndpoint();
}
@AfterEach
public void tearDown() {
mockServerConfig.verifyHelloWorldEndpoint();
}
@AfterAll
public void tearDown() {
mockServerConfig.stopServer();
}
@Test
void shouldReturnSuccessfulResponseOnValidRequest() throws Exception {
MvcResult result = mockMvc.perform(
get("/demo")
.contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isOk())
.andReturn();
String actualResponse = result
.getResponse()
.getContentAsString();
assertThat(actualResponse).isEqualToIgnoringWhitespace("Mocked Response!!!");

}
}
api.url.helloWorld=http://localhost:1080/api/helloWorld

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store