Spring @Autowired ApplicationContext在集成测试中为什么为null?

huangapple go评论78阅读模式
英文:

Why is Spring @Autowired ApplicationContext null in integration test?

问题

我对Spring还不太熟悉,已经查看了Stack Overflow上类似的问题,但无法从中找到答案。当我调用我的applicationContext变量时,我收到了一个NPE,这意味着Bean可能没有正确创建或注入。这是我的程序的相关部分:

@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class SampleIT {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void sampleMethod() throws Exception {

        //下面会出现NPE
        String[] allBeanNames = applicationContext.getBeanDefinitionNames();

        //更多逻辑
    }
}

我尝试获取ApplicationContext实例以调试为什么来自我的配置的其他Bean为空,所以这一切肯定是因为我对ApplicationContext设置以及从中注入Bean的理解存在基本缺陷。非常感谢任何帮助!

英文:

I'm still new to Spring and have looked at similar questions on stack but couldn't identify an answer from what I've read.

I'm receiving an NPE when I call my applicationContext variable, which means the bean must not have been created or injected correctly. Here is the relevant portion of my program.

@SpringBootTest
@ContextConfiguration(classes = TestConfig.class)
public class SampleIT {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void sampleMethod() throws Exception {

        //NPE below
        String[] allBeanNames = applicationContext.getBeanDefinitionNames();

        //more logic 

I'm trying to get the ApplicationContext instance to debug why other beans are null from my config, so this all must be because of a basic flaw in my understanding of how ApplicationContext is setup and beans from it are injected. Any help is very much appreciated!

答案1

得分: 8

尝试将以下注解添加到您的类 SampleIT 中:

@RunWith(SpringRunner.class)
英文:

Try to add the following annotation to your class SampleIT:

@RunWith(SpringRunner.class)

答案2

得分: 1

导入org.junit.jupiter.api.Test而不是org.junit.Test将导致使用Junit5。这将允许Junit与Spring一起工作,无需@RunWith(SpringRunner.class)注解。在我正在处理的代码库中仍然存在Junit4的痕迹,因此这是在完全迁移到Junit 5之前的一个快速修复。

英文:

Answering my own question now. Importing org.junit.jupiter.api.Test instead of org.junit.Test will cause Junit5 to be used. This will allow Junit to work with Spring without the @RunWith(SpringRunner.class) annotation. There are still remnants of Junit4 left in the codebase I'm working in, so this is a quick fix before completely moving to Junit 5.

答案3

得分: 1

如果您正在使用 Junit 4 版本,您需要使用 @RunWith(SpringRunner.class),
如果您正在使用 Junit 5 版本,您需要使用 @ExtendWith(SpringExtension.class)。

另外,如果您正在使用 Mockito,您可以使用 @ExtendWith(MockitoExtension.class) 等。

我强烈建议阅读有关 @SpringBootTest 注解的文档,链接如下:
@SpringBootTest

英文:

If you are using Junit in version 4, You have to use @RunWith(SpringRunner.class),
If you are using Junit version 5, You have to use @ExtendWith(SpringExtension.class).

Additionally if you are using Mockito You can use @ExtendWith(MockitoExtension.class) etc.

I highly recommend read documentation about @SpringBootTest annotation in this link
@SpringBootTest

huangapple
  • 本文由 发表于 2020年8月6日 00:18:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63269360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定