简体   繁体   English

Bean 自动装配为 null 用于使用 spring 引导 2.1.15.RELEASE(junit 4)进行集成测试

[英]Bean autowired as null for integration test using spring boot 2.1.15.RELEASE(junit 4)

Following is my code that is causing issue and it is using spring-boot-starter-parent 2.1.15.RELEASE(this version has junit 4 in it).In this context,summary,title all beans are injected as null.以下是导致问题的我的代码,它使用 spring-boot-starter-parent 2.1.15.RELEASE(此版本中有 junit 4)。在这种情况下,总结,标题所有 bean 都被注入为 null。

import com.example.demo.helpers.Subtitle;
import com.example.demo.helpers.Summary;
import com.example.demo.helpers.Title;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

@SpringBootTest(classes = {Summary.class, Title.class, Subtitle.class},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(initializers = org.springframework.boot.test.context.ConfigFileApplicationContextInitializer.class)
public class DemoApplicationTests {

    @Autowired
    ApplicationContext context;  // null

    @Autowired
    Summary summary;    // null

    @Autowired
    Title title;        // null

    @Test
    public void contextLoads() {
        System.out.println("executed testcase..");
    }
}

However when i upgrade the version of spring-boot-starter-parent to 2.6.1(it has junit 5 in it).All autowired beans are getting injected properly.但是,当我将 spring-boot-starter-parent 的版本升级到 2.6.1 时(其中包含 junit 5)。所有自动装配的 bean 都被正确注入。

However there is certain limitation that i can not upgrade version as of now.但是有一定的限制,我现在无法升级版本。

You're missing你不见了

@RunWith(SpringRunner.class)

Otherwise Autowired doesn't work on JUnit 4否则自动接线在 JUnit 4 上不起作用

see SpringRunner vs SpringBootTestSpringRunner 与 SpringBootTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Autowired Bean 在 Spring Boot 单元测试中为 NULL - @Autowired Bean is NULL in Spring Boot Unit Test Spring Boot应用程序中的JUnit测试中没有自动装配JPA存储库的合格Bean - No qualifying bean for autowired JPA repository in JUnit test in Spring Boot application spring 启动 - 集成测试自动接线接口未找到此类 bean - spring boot - integration test autowired interface no such bean found 有没有办法在使用Spring或Spring Boot的JUnit测试中使用Autowired构造函数? - Is there a way to use Autowired constructor in JUnit test using Spring or Spring Boot? Autowired MockMvc 是 null 与 JUnit 5 和 Spring 启动 - Autowired MockMvc is null with JUnit 5 and Spring boot 升级到Spring 3和junit 11-运行测试后上下文变为空。 没有可用于第二次测试的自动装配的Bean - Upgraded to Spring 3 and junit 11 - Context becoming null after running a test. No autowired bean available for the second test Spring Boot JPA @AutoWired bean null - Spring Boot JPA @AutoWired bean null 春季启动集成测试-数据库未与@WebMvcTest自动关联 - spring boot integration test - database not autowired with @WebMvcTest 为什么 Spring @Autowired ApplicationContext null 在集成测试中? - Why is Spring @Autowired ApplicationContext null in integration test? Spring Boot 测试中的自动装配控制器为空 - Autowired Controller in Spring Boot test is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM