Adding expectations for the rendered output reveals annother misconception
authorKai Moritz <kai@juplo.de>
Fri, 2 Oct 2020 13:46:09 +0000 (15:46 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 2 Oct 2020 17:19:05 +0000 (19:19 +0200)
* Added Jsoup to check some expectations regarding the rendered pages.
* This revealed another misconception about the behaviour of MockMvc.
* The MockMvc-environment does not render a full version of the White Label
  ErrorPage, that Spring-Boot shows for uncaught exceptions.

pom.xml
src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java

diff --git a/pom.xml b/pom.xml
index e0b19e8..087036f 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                                </exclusion>
                        </exclusions>
                </dependency>
+               <dependency>
+                       <groupId>org.jsoup</groupId>
+                       <artifactId>jsoup</artifactId>
+                       <version>1.13.1</version>
+                       <scope>test</scope>
+               </dependency>
        </dependencies>
 
        <build>
index 65d3e4a..b09f2c7 100644 (file)
@@ -1,5 +1,7 @@
 package de.juplo.demo;
 
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -11,6 +13,7 @@ import org.springframework.test.web.servlet.MockMvc;
 import java.net.URI;
 import java.util.Optional;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.*;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -36,7 +39,12 @@ class ExceptionHandlingApplicationTests {
        void test200ForNoAnswer() throws Exception {
                mvc
                                .perform(get(URI.create("http://FOO/")))
-                               .andExpect(status().isOk());
+                               .andExpect(status().isOk())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.select("ul > li")).isEmpty();
+                               });
 
                verify(service, times(0)).checkAnswer(anyInt());
        }
@@ -45,7 +53,12 @@ class ExceptionHandlingApplicationTests {
        void test200ForEmptyAnswer() throws Exception {
                mvc
                                .perform(get(URI.create("http://FOO/?answer=")))
-                               .andExpect(status().isOk());
+                               .andExpect(status().isOk())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.select("ul > li")).isEmpty();
+                               });
 
                verify(service, times(0)).checkAnswer(anyInt());
        }
@@ -54,7 +67,12 @@ class ExceptionHandlingApplicationTests {
        void test200ForAnswerThatContainsOnlyWhitespace() throws Exception {
                mvc
                                .perform(get(URI.create("http://FOO/?answer=%20")))
-                               .andExpect(status().isOk());
+                               .andExpect(status().isOk())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.select("ul > li")).isEmpty();
+                               });
 
                verify(service, times(0)).checkAnswer(anyInt());
        }
@@ -65,7 +83,12 @@ class ExceptionHandlingApplicationTests {
 
                mvc
                                .perform(get(URI.create("http://FOO/?answer=1234")))
-                               .andExpect(status().isOk());
+                               .andExpect(status().isOk())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.selectFirst("ul > li:nth-child(2) > strong").text()).isEqualTo("false");
+                               });
 
                verify(service, times(1)).checkAnswer(anyInt());
        }
@@ -76,7 +99,12 @@ class ExceptionHandlingApplicationTests {
 
                mvc
                                .perform(get(URI.create("http://FOO/?answer=1234")))
-                               .andExpect(status().isOk());
+                               .andExpect(status().isOk())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.selectFirst("ul > li:nth-child(2) > strong").text()).isEqualTo("true");
+                               });
 
                verify(service, times(1)).checkAnswer(anyInt());
        }
@@ -94,18 +122,33 @@ class ExceptionHandlingApplicationTests {
                // and is thrown in the call to perform()!
                mvc
                                .perform(get(URI.create("http://FOO/?answer=1234")))
-                               .andExpect(status().isBadRequest());
+                               .andExpect(status().isBadRequest())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.selectFirst("div:nth-child(2)").text())
+                                                       .isEqualTo("There was an unexpected error (type=Internal Server Error, status=400).");
+                               });
 
                verify(service, times(1)).checkAnswer(anyInt());
        }
 
        @Test
-       void test400ForStringInput() throws Exception {
+       void test400ForStringInput_NOT_WORKING() throws Exception {
                when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
 
+               // The expected behaviour of the following test is, that the WhiteLabel Error Page
+               // is rendered, for the unallowed string-value.
+               // Instead, an almost empty page is rendered for the error.
                mvc
                                .perform(get(URI.create("http://FOO/?answer=bar")))
-                               .andExpect(status().isBadRequest());
+                               .andExpect(status().isBadRequest())
+                               .andDo((result) -> {
+                                       String content = result.getResponse().getContentAsString();
+                                       Document doc = Jsoup.parse(content);
+                                       assertThat(doc.selectFirst("div:nth-child(2)").text())
+                                                       .isEqualTo("There was an unexpected error (type=Internal Server Error, status=400).");
+                               });
 
                verify(service, times(0)).checkAnswer(anyInt());
        }