|
| 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 | +} |
0 commit comments