Initial Commit: Spring-Boot-Project, that pinns down a bug in a test-case
[demos/testing] / src / test / java / de / juplo / ThymeleafTestApplicationTests.java
1 package de.juplo;
2
3 import org.junit.jupiter.api.Test;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.test.web.servlet.MockMvc;
11 import org.springframework.test.web.servlet.MvcResult;
12
13 import java.net.URI;
14
15 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
16 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17
18 @SpringBootTest
19 @AutoConfigureWebMvc
20 @AutoConfigureMockMvc
21 class ThymeleafTestApplicationTests {
22         private final static Logger LOG =
23                         LoggerFactory.getLogger(ThymeleafTestApplicationTests.class);
24
25         @Autowired
26         MockMvc mvc;
27
28
29         @Test
30         void contextLoads() throws Exception {
31         }
32
33         @Test
34         void requests() throws Exception {
35                 mvc
36                                 .perform(get(URI.create("http://test/controller.html?template=remote")))
37                                 .andExpect(status().isOk());
38                 mvc
39                                 .perform(get(URI.create("http://test/controller.html?template=local")))
40                                 .andExpect(status().isOk());
41                 // The expected behaviour of the following test is, that the
42                 // TemplateInputException, that is thrown by Thymeleaf because of the non-existent
43                 // template-resource, is catched and reported as 503 Internal Server Error, as it
44                 // happens in the real Spring-MVC environment, when you start the web-server and
45                 // trigger the error manually.
46                 // Instead, the exception bubbles up, becomes wrapped in a NestedServletException
47                 // and is thrown in the call to perform()!
48                 mvc
49                                 .perform(get(URI.create("http://test/controller.html?template=foo")))
50                                 .andExpect(status().isInternalServerError());
51         }
52
53 }