[英]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 SpringBootTest见SpringRunner 与 SpringBootTest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.