|
3 | 3 | import com.bobocode.config.RootConfig; |
4 | 4 | import com.bobocode.config.WebConfig; |
5 | 5 | import com.bobocode.web.controller.WelcomeController; |
6 | | -import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.*; |
7 | 7 | import org.springframework.context.annotation.ComponentScan; |
8 | 8 | import org.springframework.context.annotation.ComponentScan.Filter; |
9 | 9 | import org.springframework.context.annotation.Configuration; |
10 | 10 | import org.springframework.context.annotation.FilterType; |
11 | 11 | import org.springframework.stereotype.Controller; |
12 | 12 | import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.ResponseBody; |
13 | 14 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
14 | 15 |
|
15 | 16 | import java.util.List; |
16 | 17 | import java.util.stream.Collectors; |
17 | 18 | import java.util.stream.Stream; |
18 | 19 |
|
19 | | -import static org.hamcrest.MatcherAssert.assertThat; |
20 | | -import static org.hamcrest.Matchers.arrayContaining; |
21 | | -import static org.hamcrest.Matchers.arrayWithSize; |
22 | | -import static org.hamcrest.Matchers.containsInAnyOrder; |
23 | | -import static org.hamcrest.Matchers.equalTo; |
24 | | -import static org.hamcrest.Matchers.notNullValue; |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
25 | 22 |
|
| 23 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
26 | 24 | class WelcomeWebAppTest { |
27 | 25 |
|
28 | 26 | @Test |
29 | | - void testDispatcherServletMapping() { |
30 | | - WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
31 | | - |
32 | | - assertThat(webAppInitializerWrapper.getServletMappings(), arrayContaining("/")); |
33 | | - } |
34 | | - |
35 | | - @Test |
36 | | - void testInitializerRootConfigClasses() { |
37 | | - WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
38 | | - |
39 | | - assertThat(webAppInitializerWrapper.getRootConfigClasses(), arrayContaining(RootConfig.class)); |
40 | | - } |
41 | | - |
42 | | - @Test |
43 | | - void testInitializerWebConfigClasses() { |
44 | | - WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
45 | | - |
46 | | - assertThat(webAppInitializerWrapper.getServletConfigClasses(), arrayContaining(WebConfig.class)); |
47 | | - } |
48 | | - |
49 | | - @Test |
50 | | - void testRootConfigClassIsMarkedAsConfiguration() { |
| 27 | + @Order(1) |
| 28 | + @DisplayName("RootConfig class is marked as @Configuration") |
| 29 | + void rootConfigClassIsMarkedAsConfiguration() { |
51 | 30 | Configuration configuration = RootConfig.class.getAnnotation(Configuration.class); |
52 | 31 |
|
53 | | - assertThat(configuration, notNullValue()); |
| 32 | + assertNotNull(configuration); |
54 | 33 | } |
55 | 34 |
|
56 | 35 | @Test |
57 | | - void testRootConfigClassEnablesComponentScan() { |
| 36 | + @Order(2) |
| 37 | + @DisplayName("RootConfig class enables @ComponentScan") |
| 38 | + void rootConfigClassEnablesComponentScan() { |
58 | 39 | ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
59 | 40 |
|
60 | | - assertThat(componentScan, notNullValue()); |
| 41 | + assertNotNull(componentScan); |
61 | 42 | } |
62 | 43 |
|
63 | 44 | @Test |
64 | | - void testRootConfigComponentScanPackages() { |
| 45 | + @Order(3) |
| 46 | + @DisplayName("RootConfig @ComponentScan contains base packages") |
| 47 | + void rootConfigComponentScanPackages() { |
65 | 48 | ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
66 | 49 | String[] packages = componentScan.basePackages(); |
67 | 50 | if (packages.length == 0) { |
68 | 51 | packages = componentScan.value(); |
69 | 52 | } |
70 | 53 |
|
71 | | - assertThat(packages, arrayContaining("com.bobocode")); |
| 54 | + assertThat(packages).contains("com.bobocode"); |
72 | 55 | } |
73 | 56 |
|
74 | 57 | @Test |
75 | | - void testRootConfigComponentScanFilters() { |
| 58 | + @Order(4) |
| 59 | + @DisplayName("RootConfig @ComponentScan contains web scan filters") |
| 60 | + void rootConfigComponentScanFilters() { |
76 | 61 | ComponentScan componentScan = RootConfig.class.getAnnotation(ComponentScan.class); |
77 | 62 | Filter[] filters = componentScan.excludeFilters(); |
78 | 63 | List<Class> filteredClasses = getFilteredClasses(filters); |
79 | 64 |
|
80 | | - assertThat(filters, arrayWithSize(2)); |
81 | | - assertThat(filters[0].type(), equalTo(FilterType.ANNOTATION)); |
82 | | - assertThat(filters[1].type(), equalTo(FilterType.ANNOTATION)); |
83 | | - assertThat(filteredClasses, containsInAnyOrder(EnableWebMvc.class, Controller.class)); |
84 | | - } |
85 | | - |
86 | | - private List<Class> getFilteredClasses(Filter[] filters) { |
87 | | - return Stream.of(filters).flatMap(filter -> Stream.of(filter.value())).collect(Collectors.toList()); |
| 65 | + assertThat(filters.length).isEqualTo(2); |
| 66 | + assertThat(filters[0].type()).isEqualTo(FilterType.ANNOTATION); |
| 67 | + assertThat(filters[1].type()).isEqualTo(FilterType.ANNOTATION); |
| 68 | + assertThat(filteredClasses.toArray()).containsExactlyInAnyOrder(EnableWebMvc.class, Controller.class); |
88 | 69 | } |
89 | 70 |
|
90 | 71 | @Test |
91 | | - void testWebConfigIsMarkedAsConfiguration() { |
| 72 | + @Order(5) |
| 73 | + @DisplayName("WebConfig is marked as @Configuration") |
| 74 | + void webConfigIsMarkedAsConfiguration() { |
92 | 75 | Configuration configuration = WebConfig.class.getAnnotation(Configuration.class); |
93 | 76 |
|
94 | | - assertThat(configuration, notNullValue()); |
| 77 | + assertNotNull(configuration); |
95 | 78 | } |
96 | 79 |
|
97 | 80 | @Test |
98 | | - void testWebConfigEnablesComponentScan() { |
| 81 | + @Order(6) |
| 82 | + @DisplayName("WebConfig enables @ComponentScan") |
| 83 | + void webConfigEnablesComponentScan() { |
99 | 84 | ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class); |
100 | 85 |
|
101 | | - assertThat(componentScan, notNullValue()); |
| 86 | + assertNotNull(componentScan); |
102 | 87 | } |
103 | 88 |
|
104 | 89 | @Test |
105 | | - void testWebConfigComponentScanPackages() { |
| 90 | + @Order(7) |
| 91 | + @DisplayName("WebConfig @ComponentScan contains web packages") |
| 92 | + void webConfigComponentScanPackages() { |
106 | 93 | ComponentScan componentScan = WebConfig.class.getAnnotation(ComponentScan.class); |
107 | 94 |
|
108 | | - assertThat(componentScan.basePackages(), arrayContaining("com.bobocode.web")); |
| 95 | + assertThat(componentScan.basePackages()).contains("com.bobocode.web"); |
109 | 96 | } |
110 | 97 |
|
111 | 98 | @Test |
112 | | - void testWebConfigEnablesWebMvc() { |
| 99 | + @Order(8) |
| 100 | + @DisplayName("WebConfig is marked with @EnableWebMvc") |
| 101 | + void webConfigEnablesWebMvc() { |
113 | 102 | EnableWebMvc enableWebMvc = WebConfig.class.getAnnotation(EnableWebMvc.class); |
114 | 103 |
|
115 | | - assertThat(enableWebMvc, notNullValue()); |
| 104 | + assertNotNull(enableWebMvc); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + @Order(9) |
| 109 | + @DisplayName("Initializer method is overridden with RootConfig class") |
| 110 | + void initializerRootConfigClasses() { |
| 111 | + WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
| 112 | + |
| 113 | + assertThat(webAppInitializerWrapper.getRootConfigClasses()).contains(RootConfig.class); |
116 | 114 | } |
117 | 115 |
|
118 | 116 | @Test |
119 | | - void testWelcomeControllerIsMarkedAsController() { |
| 117 | + @Order(10) |
| 118 | + @DisplayName("Initializer method is overridden with WebConfig class") |
| 119 | + void initializerWebConfigClasses() { |
| 120 | + WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
| 121 | + |
| 122 | + assertThat(webAppInitializerWrapper.getServletConfigClasses()).contains(WebConfig.class); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + @Order(11) |
| 127 | + @DisplayName("Proper Dispatcher Servlet mapping") |
| 128 | + void dispatcherServletMapping() { |
| 129 | + WebAppInitializerWrapper webAppInitializerWrapper = new WebAppInitializerWrapper(); |
| 130 | + |
| 131 | + assertThat(webAppInitializerWrapper.getServletMappings()).contains("/"); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + @Order(12) |
| 136 | + @DisplayName("WelcomeController is marked as @Controller") |
| 137 | + void welcomeControllerIsMarkedAsController() { |
120 | 138 | Controller controller = WelcomeController.class.getAnnotation(Controller.class); |
121 | 139 |
|
122 | | - assertThat(controller, notNullValue()); |
| 140 | + assertNotNull(controller); |
123 | 141 | } |
124 | 142 |
|
125 | 143 | @Test |
126 | | - void testWelcomeControllerMethodIsMarkedAsGetMethod() throws NoSuchMethodException { |
| 144 | + @Order(13) |
| 145 | + @DisplayName("WelcomeController method is marked as Get method") |
| 146 | + void welcomeControllerMethodIsMarkedAsGetMethod() throws NoSuchMethodException { |
127 | 147 | GetMapping getMapping = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(GetMapping.class); |
128 | 148 |
|
129 | | - assertThat(getMapping, notNullValue()); |
| 149 | + assertNotNull(getMapping); |
130 | 150 | } |
131 | 151 |
|
132 | 152 | @Test |
133 | | - void testWelcomeControllerMethodMapping() throws NoSuchMethodException { |
| 153 | + @Order(14) |
| 154 | + @DisplayName("WelcomeController Get method is marked properly") |
| 155 | + void welcomeControllerMethodMapping() throws NoSuchMethodException { |
134 | 156 | GetMapping getMapping = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(GetMapping.class); |
135 | 157 |
|
136 | | - assertThat(getMapping.value(), arrayContaining("/welcome")); |
| 158 | + assertThat(getMapping.value()).contains("/welcome"); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + @Order(15) |
| 163 | + @DisplayName("WelcomeController Get method is marked as @ResponseBody") |
| 164 | + void welcomeControllerMethodIsMarkedAsResponseBody() throws NoSuchMethodException { |
| 165 | + ResponseBody responseBody = WelcomeController.class.getDeclaredMethod("welcome").getAnnotation(ResponseBody.class); |
| 166 | + |
| 167 | + assertNotNull(responseBody); |
| 168 | + } |
| 169 | + |
| 170 | + private List<Class> getFilteredClasses(Filter[] filters) { |
| 171 | + return Stream.of(filters).flatMap(filter -> Stream.of(filter.value())).collect(Collectors.toList()); |
137 | 172 | } |
138 | 173 | } |
0 commit comments