3 import org.jsoup.Jsoup;
4 import org.jsoup.nodes.Document;
5 import org.junit.jupiter.api.Test;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10 import org.springframework.boot.test.mock.mockito.MockBean;
11 import org.springframework.test.web.servlet.MockMvc;
12 import org.springframework.web.util.NestedServletException;
15 import java.util.Optional;
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.mockito.ArgumentMatchers.anyInt;
19 import static org.mockito.Mockito.*;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
22 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24 @WebMvcTest(ExampleController.class)
25 class ExceptionHandlingApplicationTests {
26 private final static Logger LOG =
27 LoggerFactory.getLogger(ExceptionHandlingApplicationTests.class);
30 ExampleService service;
37 void contextLoads() throws Exception {
41 void test200ForNoAnswer() throws Exception {
43 .perform(get(URI.create("http://FOO/")))
44 .andExpect(status().isOk())
46 String content = result.getResponse().getContentAsString();
47 Document doc = Jsoup.parse(content);
48 assertThat(doc.select("ul > li")).isEmpty();
51 verify(service, times(0)).checkAnswer(anyInt());
55 void test200ForEmptyAnswer() throws Exception {
57 .perform(get(URI.create("http://FOO/?answer=")))
58 .andExpect(status().isOk())
60 String content = result.getResponse().getContentAsString();
61 Document doc = Jsoup.parse(content);
62 assertThat(doc.select("ul > li")).isEmpty();
65 verify(service, times(0)).checkAnswer(anyInt());
69 void test200ForAnswerThatContainsOnlyWhitespace() throws Exception {
71 .perform(get(URI.create("http://FOO/?answer=%20")))
72 .andExpect(status().isOk())
74 String content = result.getResponse().getContentAsString();
75 Document doc = Jsoup.parse(content);
76 assertThat(doc.select("ul > li")).isEmpty();
79 verify(service, times(0)).checkAnswer(anyInt());
83 void test200ForWrongAnswer() throws Exception {
84 when(service.checkAnswer(anyInt())).thenReturn(Optional.of(false));
87 .perform(get(URI.create("http://FOO/?answer=1234")))
88 .andExpect(status().isOk())
90 String content = result.getResponse().getContentAsString();
91 Document doc = Jsoup.parse(content);
92 assertThat(doc.selectFirst("ul > li:nth-child(2) > strong").text()).isEqualTo("false");
95 verify(service, times(1)).checkAnswer(anyInt());
99 void test200ForCorrectAnswer() throws Exception {
100 when(service.checkAnswer(anyInt())).thenReturn(Optional.of(true));
103 .perform(get(URI.create("http://FOO/?answer=1234")))
104 .andExpect(status().isOk())
106 String content = result.getResponse().getContentAsString();
107 Document doc = Jsoup.parse(content);
108 assertThat(doc.selectFirst("ul > li:nth-child(2) > strong").text()).isEqualTo("true");
111 verify(service, times(1)).checkAnswer(anyInt());
115 void testExceptionForNegativeAnswer() throws Exception {
116 when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
118 // The corrected version of the test catches the wrapper NestedServletException
119 // for exceptions, that are handled in the DispatcherServlet in a fully setup
120 // and, hence, cannot be handled in a @WebMvcTest, that mocks this part of the stack.
122 NestedServletException.class,
123 () -> mvc.perform(get(URI.create("http://FOO/?answer=1234"))));
125 verify(service, times(1)).checkAnswer(anyInt());
129 void test400ForStringInput() throws Exception {
130 when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
132 // The expected behaviour of the following test is, that the WhiteLabel Error Page
133 // is rendered, for the unallowed string-value.
134 // Instead, an almost empty page is rendered for the error.
136 .perform(get(URI.create("http://FOO/?answer=bar")))
137 .andExpect(status().isBadRequest());
138 // Specifying exceptations for the rendered output is not useful,
139 // because MockMvc does not render the White Label ErrorPage
141 verify(service, times(0)).checkAnswer(anyInt());
145 void test400ForExceptionInBusinessLogic() throws Exception {
146 when(service.checkAnswer(anyInt())).thenThrow(new IllegalArgumentException("FOO!"));
149 .perform(get(URI.create("http://FOO/?answer=1234")))
150 .andExpect(status().isBadRequest())
152 String content = result.getResponse().getContentAsString();
153 Document doc = Jsoup.parse(content);
154 assertThat(doc.selectFirst("title").text())
155 .isEqualTo("400: IllegalArgumentException");
156 assertThat(doc.selectFirst("div > p > strong").text())
157 .isEqualTo("Catched exception: java.lang.IllegalArgumentException: FOO!");
160 verify(service, times(1)).checkAnswer(anyInt());