|
| 1 | +package com.bobocode.viewresolver; |
| 2 | + |
| 3 | +import com.bobocode.viewresolver.config.ResolverConfig; |
| 4 | +import lombok.SneakyThrows; |
| 5 | +import org.junit.jupiter.api.*; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 8 | +import org.springframework.context.annotation.ComponentScan; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.test.context.ContextConfiguration; |
| 11 | +import org.springframework.test.web.servlet.MockMvc; |
| 12 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 13 | +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; |
| 14 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 15 | +import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; |
| 16 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 17 | + |
| 18 | +import java.lang.reflect.Method; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.*; |
| 21 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 23 | + |
| 24 | +@WebMvcTest |
| 25 | +@ContextConfiguration(classes = {ResolverConfig.class}) |
| 26 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 27 | +public class ViewResolverTest { |
| 28 | + @Autowired |
| 29 | + private MockMvc mockMvc; |
| 30 | + |
| 31 | + @Test |
| 32 | + @Order(1) |
| 33 | + @DisplayName("Class is marked as @Configuration") |
| 34 | + void classIsMarkedAsConfiguration() { |
| 35 | + Configuration configuration = ResolverConfig.class.getAnnotation(Configuration.class); |
| 36 | + assertNotNull(configuration); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + @Order(2) |
| 41 | + @DisplayName("Configuration Class marked as @EnableWebMvc") |
| 42 | + void classAnnotatedWithEnableWebMvc() { |
| 43 | + EnableWebMvc enableWebMvc = ResolverConfig.class.getAnnotation(EnableWebMvc.class); |
| 44 | + assertNotNull(enableWebMvc); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @Order(3) |
| 49 | + @DisplayName("Configuration Class marked as @ComponentScan") |
| 50 | + void classAnnotatedWithComponentScan() { |
| 51 | + ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class); |
| 52 | + assertNotNull(componentScan); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @Order(4) |
| 57 | + @DisplayName("ComponentScan packages are indicated") |
| 58 | + void ComponentScanHasIndicatedPackages() { |
| 59 | + ComponentScan componentScan = ResolverConfig.class.getAnnotation(ComponentScan.class); |
| 60 | + assertEquals("com.bobocode.resolver", componentScan.basePackages()[0]); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @Order(5) |
| 65 | + @DisplayName("Is config class implements WebMvcConfigurer interface") |
| 66 | + void isConfigClassImplementsWebMvcConfigurerInterface() { |
| 67 | + assertTrue(WebMvcConfigurer.class.isAssignableFrom(ResolverConfig.class)); |
| 68 | + } |
| 69 | + |
| 70 | + @SneakyThrows |
| 71 | + @Test |
| 72 | + @Order(6) |
| 73 | + @DisplayName("Is config class overrides configureViewResolvers method") |
| 74 | + void isConfigurationClassContainsViewResolverMethod() { |
| 75 | + Method configureViewResolvers = ResolverConfig.class.getMethod("configureViewResolvers", ViewResolverRegistry.class); |
| 76 | + assertNotNull(configureViewResolvers); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @Order(7) |
| 81 | + @SneakyThrows |
| 82 | + void getRequestReturnOkStatus() { |
| 83 | + mockMvc.perform(get("/")) |
| 84 | + .andExpect(status().isOk()); |
| 85 | + } |
| 86 | +} |
0 commit comments