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;
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;
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());
}
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());
}
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());
}
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());
}
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());
}
// 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());
}