【发布时间】:2017-05-21 14:13:05
【问题描述】:
这是我的 Spring 控制器测试用例
@RunWith(SpringRunner.class)
@WebMvcTest(value = MyController.class)
public class MyControllerTest {
@MockBean
private MyService myService;
}
所以这是一个专门针对 MyController 中的方法的单元测试。但是当我运行测试时,Spring 似乎开始实例化 OtherController 及其所有依赖项。
我已尝试将以上内容更新为
@RunWith(SpringRunner.class)
@WebMvcTest(value = MyController.class, excludeFilters = @ComponentScan.Filter(value= OtherController.class, type = FilterType.ANNOTATION))
public class MyControllerTest {
...
}
但弹簧似乎仍然在连接它。这是 Spring 在我专门运行上述测试时尝试实例化 OtherController 时引发的错误。
2017-01-06 12:09:46.207 WARN 18092 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'otherController' defined in file [C:\..OtherController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getOtherService' defined in com.my.myApplication: Unsatisfied dependency expressed through method 'getOtherService' parameter 0org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getOtherService' defined in com.myOrg.MyServiceApplication: Unsatisfied dependency expressed through method 'getPositionService' parameter 0
这可能是什么原因造成的?
【问题讨论】:
-
我知道已经有一段时间了,你解决了吗?你能显示 MyController 和 OtherController 吗?
标签: java spring unit-testing spring-boot spring-test-mvc