From a45ca1cc78cc207eb165c203e0295b41852f4f58 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 2 Oct 2020 15:46:09 +0200 Subject: [PATCH] Adding expectations for the rendered output reveals annother misconception * 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 | 6 ++ .../ExceptionHandlingApplicationTests.java | 59 ++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index e0b19e8..087036f 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,12 @@ + + org.jsoup + jsoup + 1.13.1 + test + diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java index 65d3e4a..b09f2c7 100644 --- a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -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()); } -- 2.20.1