控制器的 Spring Boot 测试 @WebMvcTest 似乎在上下文中加载其他控制器 [英] Spring Boot Testing @WebMvcTest for a Controller appears to load other controllers in the context

查看:114
本文介绍了控制器的 Spring Boot 测试 @WebMvcTest 似乎在上下文中加载其他控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 Spring Controller 测试用例

Here's my test case for a Spring Controller

@RunWith(SpringRunner.class)
@WebMvcTest(value = MyController.class)
public class MyControllerTest {

    @MockBean
    private MyService myService;
}

所以这是一个专门针对 MyController 中的方法的单元测试.但是当我运行测试时,Spring 似乎开始实例化 OtherController 及其所有依赖项.

So this is a unit test specifically for the methods in MyController. But when I run the test, Spring appears to begin instantiating OtherController and all of it's dependencies.

我已经尝试将上述内容更新为

I have tried updating the above as

@RunWith(SpringRunner.class)
@WebMvcTest(value = MyController.class, excludeFilters = @ComponentScan.Filter(value= OtherController.class, type = FilterType.ANNOTATION))


   public class MyControllerTest {

...
}

但弹簧似乎仍然连接它.这是 Spring 在我专门运行上述测试时尝试实例化 OtherController 时抛出的错误.

But spring still appears to wire it. Here's the error thrown by Spring as it tries to instantiate OtherController when I run the above test specifically.

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

这可能是什么原因造成的?

What could be causing this?

推荐答案

可能是您通过 @Componentscan 或类似方法意外触发了 bean 扫描.

Chances are that you are triggering the bean scanning by accident via a @Componentscan or the likes.

例如,如 这个答案,Spring 可能正在检测您的生产"@SpringBootApplication Application 类,以及它带来的所有组件扫描.如果是这样,请确保您用测试"一个推杆覆盖"您的生产"应用程序类......

For instance, as explained in this answer, Spring may be detecting your "production" @SpringBootApplication Application class, with all the component scans it brings about. If so make sure you "overwrite" your "production" Application class with your "test" one putting...

@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

...在会覆盖生产"应用程序的位置.

...in a location that would override the "Production" Application.

例如,如果您的 IDE 没有被类名冲突弄乱:

For instance, if your IDE doesn't get messed by class name clashes:

"production" -> src/main/java/com/mycompany/Application.java
             -> src/main/java/com/mycompany/controllers/MyController.java
"test"       -> src/test/java/com/mycompany/Application.java
             -> src/test/java/com/mycompany/controllers/MyControllerTest.java

作为替代,我还发现在我的情况下这也有效(即将应用程序放在测试控制器文件夹中)

As an alternative I also have found that in my case this works as well (i.e. placing the Application in the test controllers folder)

"production" -> src/main/java/com/mycompany/Application.java
             -> src/main/java/com/mycompany/controllers/MyController.java
"test"       -> src/test/java/com/mycompany/controllers/Application.java
             -> src/test/java/com/mycompany/controllers/MyControllerTest.java

这篇关于控制器的 Spring Boot 测试 @WebMvcTest 似乎在上下文中加载其他控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆