【发布时间】:2021-02-26 21:04:19
【问题描述】:
我为SystemAuthorityController 创建了一个测试类,因为我只需要加载部分上下文。
我使用了@WebMvcTest 注释并指定了要测试的控制器(我也尝试了所有控制器,但也没有用)。
@WebMvcTest(SystemAuthorityController.class)
@TestPropertySource("classpath:application.properties")
public class SystemAuthorityControllerTest
当我尝试从此控制器调用任何端点时,我得到404,因为找不到端点。
经过一番研究,我找到了解决方案 - 即在我需要的控制器中添加 @Import 注释,之后一切正常,找到了 URL。
@WebMvcTest(SystemAuthorityController.class)
@Import({SystemAuthorityController.class})
@TestPropertySource("classpath:application.properties")
public class SystemAuthorityControllerTest
我的问题是为什么我需要显式导入控制器我想测试,因为我从未见过此注释用于此目的(我也不认为我应该这样使用) .据我了解,WebMvcTest 应该加载所有控制器 bean。
【问题讨论】: