我正在尝试使用
@WebMvcTest
测试我的简单 Web 应用程序。我的应用程序中只有 2 个控制器 HomeController
和 PersonController
。我只想在一个测试类中实例化一个控制器。为此,我使用了 controllers
注释的
@WebMvcTest
字段。但看起来这个字段被忽略了,两个控制器都被实例化了。因为即使我在测试中只指定一个控制器,其他控制器的请求处理程序也会通过测试。你能告诉我,这里有什么问题吗?
我一起工作 openjdk 版本“17.0.5” springBoot 版本“3.0.4” spring-boot-starter-test 版本“3.0.4”
有2个控制器,我省略了我的配置文件。
@Controller
public class HomeController {
@RequestMapping(value = {"/home"}, method = RequestMethod.GET)
public String home(Model model)
}
}
@Controller
public class PersonController {
}
这是我的测试
@WebMvcTest(controllers = {PersonController.class})//I specify to test empty PersonController
public class MyWebMvcTest {
@Autowired
MockMvc mockMvc;
@Test
void testHomeIsOk() throws Exception {
mockMvc
.perform(get("/home"))
.andExpect(status().isOk());
// still pass the test, allthough /home request is handled in HomeController
}
}