From: Kai Moritz Date: Sat, 19 Sep 2020 12:08:38 +0000 (+0200) Subject: Added an integration-test that tests the exception-handling X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=462d5bb4005e9128cc76911049a84252d0beeec1;p=demos%2Ftesting Added an integration-test that tests the exception-handling * The integration-test uses WebTestClient to test the exception-handling * Since this is a real integration-test, the exception-handling behave exactly the same way * The test-setup with the WebTestClient needs an additional dependency, to work correctly - without it, the WebTestClient-Bean would not be intanciated * The added test ExceptionHandlingApplicationIT tests exaclty the same stuff, as ExceptionHandlingApplicationTest - this is no good test-design, but it serves the intention, to compare the two approaches * Since Spring-Boot configures the surefire-maven-plugin to ignore all tests, that end with IT, the failsafe-maven-plugin has to be added to automatically fire the new test - remember: you have to call "mvn verify" to fire IT --- diff --git a/pom.xml b/pom.xml index 3c268c3..7ec41c3 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,11 @@ + + org.springframework.boot + spring-boot-starter-webflux + test + @@ -52,6 +57,10 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-failsafe-plugin + diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationIT.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationIT.java new file mode 100644 index 0000000..042ce4b --- /dev/null +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationIT.java @@ -0,0 +1,58 @@ +package de.juplo.demo; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.reactive.server.WebTestClient; + +import java.net.URI; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@AutoConfigureWebTestClient +class ExceptionHandlingApplicationIT { + private final static Logger LOG = + LoggerFactory.getLogger(ExceptionHandlingApplicationIT.class); + + @Autowired + WebTestClient client; + + + @Test + void contextLoads() throws Exception { + } + + @Test + void test200() throws Exception { + client + .get() + .uri("/?template=a") + .exchange() + .expectStatus().isOk(); + client + .get() + .uri("/?template=b") + .exchange() + .expectStatus().isOk(); + } + + @Test + void test503ByController() throws Exception { + client + .get() + .uri("/?template=foo") + .exchange() + .expectStatus().is5xxServerError(); + } + + @Test + void test503ByInfrastructure() throws Exception { + client + .get() + .uri("/?template=boom") + .exchange() + .expectStatus().is5xxServerError(); + } +}