Fixed non-working test-cases
[demos/testing] / src / test / java / de / juplo / demo / ExceptionHandlingApplicationTests.java
1 package de.juplo.demo;
2
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;
13
14 import java.net.URI;
15 import java.util.Optional;
16
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;
23
24 @WebMvcTest(ExampleController.class)
25 class ExceptionHandlingApplicationTests {
26         private final static Logger LOG =
27                         LoggerFactory.getLogger(ExceptionHandlingApplicationTests.class);
28
29         @MockBean
30         ExampleService service;
31
32         @Autowired
33         MockMvc mvc;
34
35
36         @Test
37         void contextLoads() throws Exception {
38         }
39
40         @Test
41         void test200ForNoAnswer() throws Exception {
42                 mvc
43                                 .perform(get(URI.create("http://FOO/")))
44                                 .andExpect(status().isOk())
45                                 .andDo((result) -> {
46                                         String content = result.getResponse().getContentAsString();
47                                         Document doc = Jsoup.parse(content);
48                                         assertThat(doc.select("ul > li")).isEmpty();
49                                 });
50
51                 verify(service, times(0)).checkAnswer(anyInt());
52         }
53
54         @Test
55         void test200ForEmptyAnswer() throws Exception {
56                 mvc
57                                 .perform(get(URI.create("http://FOO/?answer=")))
58                                 .andExpect(status().isOk())
59                                 .andDo((result) -> {
60                                         String content = result.getResponse().getContentAsString();
61                                         Document doc = Jsoup.parse(content);
62                                         assertThat(doc.select("ul > li")).isEmpty();
63                                 });
64
65                 verify(service, times(0)).checkAnswer(anyInt());
66         }
67
68         @Test
69         void test200ForAnswerThatContainsOnlyWhitespace() throws Exception {
70                 mvc
71                                 .perform(get(URI.create("http://FOO/?answer=%20")))
72                                 .andExpect(status().isOk())
73                                 .andDo((result) -> {
74                                         String content = result.getResponse().getContentAsString();
75                                         Document doc = Jsoup.parse(content);
76                                         assertThat(doc.select("ul > li")).isEmpty();
77                                 });
78
79                 verify(service, times(0)).checkAnswer(anyInt());
80         }
81
82         @Test
83         void test200ForWrongAnswer() throws Exception {
84                 when(service.checkAnswer(anyInt())).thenReturn(Optional.of(false));
85
86                 mvc
87                                 .perform(get(URI.create("http://FOO/?answer=1234")))
88                                 .andExpect(status().isOk())
89                                 .andDo((result) -> {
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");
93                                 });
94
95                 verify(service, times(1)).checkAnswer(anyInt());
96         }
97
98         @Test
99         void test200ForCorrectAnswer() throws Exception {
100                 when(service.checkAnswer(anyInt())).thenReturn(Optional.of(true));
101
102                 mvc
103                                 .perform(get(URI.create("http://FOO/?answer=1234")))
104                                 .andExpect(status().isOk())
105                                 .andDo((result) -> {
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");
109                                 });
110
111                 verify(service, times(1)).checkAnswer(anyInt());
112         }
113
114         @Test
115         void testExceptionForNegativeAnswer() throws Exception {
116                 when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
117
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.
121                 assertThrows(
122                                 NestedServletException.class,
123                                 () -> mvc.perform(get(URI.create("http://FOO/?answer=1234"))));
124
125                 verify(service, times(1)).checkAnswer(anyInt());
126         }
127
128         @Test
129         void test400ForStringInput() throws Exception {
130                 when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
131
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.
135                 mvc
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
140
141                 verify(service, times(0)).checkAnswer(anyInt());
142         }
143 }