Skip to content

Commit e60b3e9

Browse files
author
Ivan Virchenko
committed
add HelloJspController tests and refactor viewResolverRegistryHasCorrectPrefixAndSuffix() method
1 parent 98b7dd8 commit e60b3e9

File tree

3 files changed

+110
-11
lines changed

3 files changed

+110
-11
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.bobocode.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
7+
@Controller
8+
@RequestMapping
9+
public class HelloJspController {
10+
@GetMapping
11+
public String hello() {
12+
return "hello";
13+
}
14+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.bobocode;
2+
3+
import com.bobocode.config.ResolverConfig;
4+
import com.bobocode.controller.HelloJspController;
5+
import lombok.SneakyThrows;
6+
import org.junit.jupiter.api.*;
7+
import org.mockito.internal.configuration.ClassPathLoader;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10+
import org.springframework.http.MediaType;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.web.servlet.MockMvc;
14+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
15+
import org.springframework.validation.support.BindingAwareModelMap;
16+
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.Arrays;
21+
22+
import static org.junit.jupiter.api.Assertions.*;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
26+
27+
@WebMvcTest
28+
@ContextConfiguration(classes = ResolverConfig.class)
29+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
30+
public class HelloJspControllerTest {
31+
32+
@Autowired
33+
private MockMvc mockMvc;
34+
private final HelloJspController helloJspController = new HelloJspController();
35+
36+
@Test
37+
@Order(1)
38+
@DisplayName("Class marked as @Controller")
39+
void helloJspControllerClassMarkedAsController() {
40+
Controller controller = HelloJspController.class.getAnnotation(Controller.class);
41+
assertNotNull(controller);
42+
}
43+
44+
@Test
45+
@Order(2)
46+
@DisplayName("Class marked as @RequestMapping")
47+
void helloJspControllerClassMarkedAsRequestMapping() {
48+
RequestMapping requestMapping = HelloJspController.class.getAnnotation(RequestMapping.class);
49+
assertNotNull(requestMapping);
50+
}
51+
52+
@Test
53+
@Order(3)
54+
@DisplayName("Method that get hello page is marker with @GetMapping")
55+
void helloJspControllerHasMethodAnnotatedAsGetMapping() {
56+
boolean hasAnnotation = Arrays.stream(HelloJspController.class.getMethods())
57+
.anyMatch(method ->
58+
method.getAnnotation(GetMapping.class) != null
59+
);
60+
assertTrue(hasAnnotation);
61+
}
62+
63+
@Test
64+
@Order(4)
65+
@SneakyThrows
66+
@DisplayName("Get method returns view name \"hello\"")
67+
void helloJspControllerMethodReturnsHelloViewName() {
68+
Method method = Arrays.stream(HelloJspController.class.getMethods())
69+
.filter(m -> m.getAnnotation(GetMapping.class) != null)
70+
.findFirst()
71+
.orElseThrow();
72+
73+
var viewName = method.invoke(helloJspController);
74+
75+
assertEquals("hello", viewName);
76+
}
77+
78+
@Test
79+
@Order(5)
80+
@SneakyThrows
81+
@DisplayName("GET method is completed ✅")
82+
void getRequestShouldReturnStatusOkAndCorrectData() {
83+
mockMvc.perform(get("/"))
84+
.andExpect(status().isOk())
85+
.andExpect(view().name("hello"))
86+
.andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/hello.jsp"));
87+
}
88+
}

3-0-spring-framework/3-0-2-view-resolver/src/test/java/com/bobocode/ViewResolverTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.bobocode.config.ResolverConfig;
44
import lombok.SneakyThrows;
55
import org.junit.jupiter.api.*;
6+
import org.mockito.Mockito;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
89
import org.springframework.context.annotation.ComponentScan;
@@ -21,13 +22,11 @@
2122
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2223
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
2324

24-
@WebMvcTest
25-
@ContextConfiguration(classes = {ResolverConfig.class})
2625
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2726
public class ViewResolverTest {
2827

29-
@Autowired
30-
private MockMvc mockMvc;
28+
private final ResolverConfig config = new ResolverConfig();
29+
private final ViewResolverRegistry resolverRegistry = Mockito.mock(ViewResolverRegistry.class);
3130

3231
@Test
3332
@Order(1)
@@ -58,7 +57,7 @@ void classAnnotatedWithComponentScan() {
5857
@DisplayName("ComponentScan packages are indicated")
5958
void ComponentScanHasIndicatedPackages() {
6059
ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class);
61-
assertEquals("com.bobocode.resolver", componentScan.basePackages()[0]);
60+
assertEquals("com.bobocode", componentScan.basePackages()[0]);
6261
}
6362

6463
@Test
@@ -80,11 +79,9 @@ void isConfigurationClassContainsViewResolverMethod() {
8079
@Test
8180
@Order(7)
8281
@SneakyThrows
83-
@DisplayName("Get request returns correct view name and URL")
84-
void getRequestReturnCorrectUrlAndViewName() {
85-
mockMvc.perform(get("/"))
86-
.andExpect(status().isOk())
87-
.andExpect(view().name("index"))
88-
.andExpect(MockMvcResultMatchers.forwardedUrl("/WEB-INF/views/index.jsp"));
82+
@DisplayName("View resolver registry has correct prefix and suffix")
83+
void viewResolverRegistryHasCorrectPrefixAndSuffix() {
84+
config.getClass().getMethod("configureViewResolvers", ViewResolverRegistry.class).invoke(config, resolverRegistry);
85+
Mockito.verify(resolverRegistry).jsp("/WEB-INF/views/", ".jsp");
8986
}
9087
}

0 commit comments

Comments
 (0)