I’ve two configurations classes:
@Configuration
@EnableWebMvc
public class WebConfig {
}
@Configuration
@ComponentScan("io.abned.spring.websocket.controllers")
public class AppConfig {
}
And a controller and its test class:
@RestController
@RequestMapping("api/v1")
public class HelloController {
@GetMapping("hello")
public String hello() {
return "Hello, world!";
}
}
@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(classes = AppConfig.class),
@ContextConfiguration(classes = WebConfig.class)
})
class HelloControllerIntegrationTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@Test
void testHello() throws Exception {
mockMvc.perform(get("/api/v1/hello"))
.andExpect(status().isOk())
.andExpect(jsonPath("$").value("Hello, world!"));
}
}
This test failed. But if I move the @ComponentScan to WebConfig then it’s succeded.
So, why it is failed if I separate @ComponentScan to @EnableWebMvc ?
PS: I use spring 6.1.10 (without spring boot)