java 在 Spring Boot 的 JUnit 测试中创建 bean 时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45863347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:56:40  来源:igfitidea点击:

Error creating bean in JUnit test in Spring Boot

javaspringspring-bootjunitdependency-injection

提问by Zychoo

I am creating application in Spring Boot. I created service that looks that way:

我正在 Spring Boot 中创建应用程序。我创建了这样的服务:

@Service
public class MyService {

    @Value("${myprops.hostname}")
    private String host;

    public void callEndpoint() {
        String endpointUrl = this.host + "/endpoint";
        System.out.println(endpointUrl);
    }
}

This service will connect to REST endpoint to other application (developed also by me) that will be deployed along. That is why I want to customize hostname in application.properties file (-default, -qa, -dev).

此服务将连接到 REST 端点,以连接到将一起部署的其他应用程序(也由我开发)。这就是为什么我想在 application.properties 文件(-default、-qa、-dev)中自定义主机名。

My application builds and works just fine. I tested it by creating controller that calls this service, and it populates hostfield with correct property from application.properties.

我的应用程序构建并运行良好。我通过创建调用此服务的控制器对其进行了测试,并host使用 application.properties 中的正确属性填充字段。

The problem occurs when I try to write test for this class. When I try this approach:

当我尝试为此类编写测试时出现问题。当我尝试这种方法时:

@RunWith(SpringRunner.class)
public class MyServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void callEndpoint() {
        myService.callEndpoint();
    }
}

i receive exception:

我收到异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ge.bm.wip.comp.processor.service.MyServiceTest': Unsatisfied dependency expressed through field 'myService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ge.bm.wip.comp.processor.service.MyService' available: expected at least 1 bean which qualifies as autowire candidate.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.ge.bm.wip.comp.processor.service.MyServiceTest': Unsatisfied dependency expressed through field 'myService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ge.bm.wip.comp.processor.service.MyService' available: expected at least 1 bean which qualifies as autowire candidate.

And some more nested exception. I can post them if it will help. I suppose that for some reason SpringRunner does not launch this test in Spring context and therefore cannot see bean MyService.

还有一些更多的嵌套异常。如果有帮助,我可以发布它们。我想由于某种原因 SpringRunner 没有在 Spring 上下文中启动这个测试,因此看不到 bean MyService。

Does anyone know how it can be fixed? I tried normal initialization:

有谁知道如何修复?我尝试了正常的初始化:

private MyService myService = new myService();

but then hostfield is null

但是host字段是null

回答by Sasha Shpota

You have to annotate your test with @SpringBootTestas well.

您还必须注释您的测试@SpringBootTest

Try:

尝试:

@SpringBootTest
@RunWith(SpringRunner.class)
public class MyServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void callEndpoint() {
        myService.callEndpoint();
    }
}