From: Kai Moritz Date: Fri, 10 Jan 2020 19:07:51 +0000 (+0100) Subject: Implemented a narrow integration-test: DemoApplicationIT X-Git-Tag: wip-it~23 X-Git-Url: https://juplo.de/gitweb/?p=demos%2Ftesting;a=commitdiff_plain;h=a82d5bd5f72e69bf3ffc53f263595885dfa47a87 Implemented a narrow integration-test: DemoApplicationIT --- diff --git a/pom.xml b/pom.xml index 5ba398c..d2dffbf 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,8 @@ 1.8 + 5.8.1 + 1.12.4 @@ -39,6 +41,23 @@ + + org.springframework.boot + spring-boot-starter-web + test + + + org.mock-server + mockserver-netty + ${mockserver.version} + test + + + org.testcontainers + nginx + ${testcontainers-nginx.version} + test + io.projectreactor reactor-test @@ -58,6 +77,10 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-failsafe-plugin + diff --git a/src/test/java/de/juplo/demo/DemoApplicationIT.java b/src/test/java/de/juplo/demo/DemoApplicationIT.java new file mode 100644 index 0000000..039b4b6 --- /dev/null +++ b/src/test/java/de/juplo/demo/DemoApplicationIT.java @@ -0,0 +1,63 @@ +package de.juplo.demo; + +import java.net.URI; +import org.junit.jupiter.api.Test; +import static org.mockserver.matchers.Times.exactly; +import static org.mockserver.model.HttpForward.forward; +import static org.mockserver.model.HttpRequest.request; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.springframework.web.reactive.function.client.WebClient; + +@SpringBootTest +@AutoConfigureMockMvc +class DemoApplicationIT extends IntegrationTestBase +{ + @Autowired + MockMvc mockMvc; + + + @Test + void test() throws Exception + { + MOCK_SERVER + .when(request().withPath("/test.txt"), exactly(1)) + .forward(forward() + .withHost(NGINX.getContainerIpAddress()) + .withPort(NGINX.getMappedPort(80))); + mockMvc + .perform(get(URI.create("http://S.U.T/?path=test.txt"))) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.TEXT_PLAIN)) + .andExpect(content().string("Hello World!\n")); + } + + @Configuration + static class Application + { + @Bean + RemoteContentService service() + { + return new RemoteContentService( + WebClient + .builder() + .baseUrl("http://localhost:" + + DemoApplicationIT.MOCK_SERVER.getLocalPort()) + .build()); + } + + @Bean + HtmlController controller(RemoteContentService service) + { + return new HtmlController(service); + } + } +} diff --git a/src/test/java/de/juplo/demo/IntegrationTestBase.java b/src/test/java/de/juplo/demo/IntegrationTestBase.java new file mode 100644 index 0000000..2135a6d --- /dev/null +++ b/src/test/java/de/juplo/demo/IntegrationTestBase.java @@ -0,0 +1,51 @@ +package de.juplo.demo; + + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.mockserver.integration.ClientAndServer; +import org.testcontainers.containers.NginxContainer; +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; + + + +/** + * This class starts a + * {@link https://www.testcontainers.org/modules/nginx/ NginX-Webserver} + * via a {@link https://www.testcontainers.org/ Testcontainer} and a + * standalone {@link http://www.mock-server.com/#what-is-mockserver MockServer}, + * that can be used to intercept requests for assertions / verifications. + *

+ * We use the {@link + * https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers + * Singleton Pattern} to start the NginX-Testcontainer and start the MockServer + * programmatically via the {@link + * http://www.mock-server.com/mock_server/running_mock_server.html#client_api + * Client-API}. + * @author Kai Moritz + */ +public abstract class IntegrationTestBase +{ + static final NginxContainer NGINX; + static final ClientAndServer MOCK_SERVER; + + + static + { + NGINX = new NginxContainer().withCustomContent("src/test/resources/"); + MOCK_SERVER = ClientAndServer.startClientAndServer(); + } + + + @BeforeAll + static void startMockServer() + { + NGINX.waitingFor(new HttpWaitStrategy()).start(); + } + + @AfterAll + static void stopMockServer() + { + MOCK_SERVER.stop(); + } +} diff --git a/src/test/resources/index.html b/src/test/resources/index.html new file mode 100644 index 0000000..f747925 --- /dev/null +++ b/src/test/resources/index.html @@ -0,0 +1 @@ +DUMMY diff --git a/src/test/resources/test.txt b/src/test/resources/test.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/src/test/resources/test.txt @@ -0,0 +1 @@ +Hello World!