Spring 启动测试 - 没有可用的“com.example.MyService"类型的合格 bean [英] Spring boot test - No qualifying bean of type 'com.example.MyService' available

查看:69
本文介绍了Spring 启动测试 - 没有可用的“com.example.MyService"类型的合格 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

stackoverflow 上有很多类似的问题,但我发现没有一个是我的情况.

There are a number of similar questions on stackoverflow, but I found none to be my case.

在我与 Spring boot 2.0.2.RELEASE 的集成测试中,我为测试创建了一个单独的 @Configuration 类,我在其中定义了 bean com.example.MyService.这个 bean 恰好被 com.example.OtherBean 中的其他 bean 使用.

In my integration test with Spring boot 2.0.2.RELEASE, I created a separate @Configuration class for the test where I did define the bean com.example.MyService. This bean happens to be used by some other bean in com.example.OtherBean.

代码如下:

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyIntegrationTestConfig.class},
        webEnvironment = SpringBootTest.WebEnvironment.MOCK)
public class MyService1Test extends MyAbstractServiceIntegrationTest {
@Test
    public void someTest() {}
}

设置和拆卸的通用摘要:

A common abstract for setup and teardown:

public class MyAbstractServiceIntegrationTest{
    @Before
    public void setUp(){}

    @After
    public void tearDown()
}

src/test 中的 MyIntegrationTestConfig,用于代替 src/main 中的配置:

MyIntegrationTestConfig in src/test, to be used instead of the configs in the src/main:

@Configuration
@ComponentScan({"com.example"})
public class MyIntegrationTestConfig {
   @Bean
   public MyService myService() {
      return null;
   }
}

MyService bean 可以为空以进行测试.

MyService bean can be null for testing purposes.

当我运行测试时,我不断收到以下错误:

When I run the test, I keep getting the following error:

没有可用的com.example.MyService"类型的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选.依赖注释:{}

No qualifying bean of type 'com.example.MyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

我什至尝试将这个内部类添加到 MyServic1Test.仍然没有帮助:

I even tried adding this inner class to MyServic1Test. Still didn't help:

@TestConfiguration
static class MyServic1TestContextConfiguration {

    @Bean(name = "MyService")
    public MyService myService() {
        return null;
    }
}

知道我在这里做错了什么吗?还是我错过了什么?

Any idea what I did wrong here? Or did I miss something?

我怀疑 Spring 在创建 src/test 文件夹中定义的 MyService bean 之前首先尝试在 src/main 文件夹中创建/自动装配 bean.可能是这样吗?或者是否有一个不同的上下文(如测试上下文),其中 bean MyService 所在,而其他 bean 位于其他上下文中并且无法定位 MyService.

My suspicion is that Spring tries to create/autowire beans in the src/main folder first before it even creates the MyService bean defined in src/test folder. Could that be the case? Or is there a different context (like test context) where bean MyService lives in, while the other beans live in an other context and could not locate MyService.

一个小问题:对于集成测试,使用 webEnvironment = SpringBootTest.WebEnvironment.MOCK 是可以的,对吗?

A side question: for integration test, it is OK to use webEnvironment = SpringBootTest.WebEnvironment.MOCK, correct?

推荐答案

问题在于您如何初始化 bean.值 null 是导致问题的那个值.就好像您实际上并未声明该对象的任何实例.为了使其工作,声明一个有效的服务实例 new MyService().

The problem is how you are initializing the bean. The value null is the one that causes the issue. It is as if you weren't infact declaring any instance of that object. To make it work declare a valid instance of the service new MyService().

这篇关于Spring 启动测试 - 没有可用的“com.example.MyService"类型的合格 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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