无法在 junit 5 中使用 @ContextConfiguration 创建用于测试的 bean
Unable to create beans for testing using @ContextConfiguration in junit 5
我正在尝试使用 Junit 5 为我的 application.After 编写 junit 测试用例,但我无法使用 @ContextConfiguration 创建必要的 bean。
它没有抛出任何错误。但是在测试中自动装配 bean 时 class 我得到了空值
我添加了以下依赖
testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.0"
testCompile('org.junit.jupiter:junit-jupiter-params:5.3.0')
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.3.0"
和我的代码
@ContextConfiguration(classes = ServiceTestContextConfiguration.class)
public class ApplicationConfigTest {
@Autowired
private ApplicationServiceConfiguration
应用服务配置;
@ContextConfiguration
public class ServiceTestContextConfiguration {
@Bean
public ApplicationServiceConfiguration applicationServiceConfiguration() {
return new ApplicationServiceConfiguration();
}
我正在使用 spring-boot 2.
可能是我们混合了 junit4 和 junit5 的导入。让我们用 @Configuration
(org.springframework.context.annotation.Configuration;
).
注释配置 class
在主单元测试中,我们使用以下内容,
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ServiceTestContextConfiguration.class)
而不是 @Before
,使用 @BeforeEach
并确保所有导入都来自 junit5(包括断言)
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
我正在尝试使用 Junit 5 为我的 application.After 编写 junit 测试用例,但我无法使用 @ContextConfiguration 创建必要的 bean。
它没有抛出任何错误。但是在测试中自动装配 bean 时 class 我得到了空值
我添加了以下依赖
testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.0"
testCompile('org.junit.jupiter:junit-jupiter-params:5.3.0')
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.3.0"
和我的代码
@ContextConfiguration(classes = ServiceTestContextConfiguration.class)
public class ApplicationConfigTest {
@Autowired
private ApplicationServiceConfiguration
应用服务配置;
@ContextConfiguration
public class ServiceTestContextConfiguration {
@Bean
public ApplicationServiceConfiguration applicationServiceConfiguration() {
return new ApplicationServiceConfiguration();
}
我正在使用 spring-boot 2.
可能是我们混合了 junit4 和 junit5 的导入。让我们用 @Configuration
(org.springframework.context.annotation.Configuration;
).
在主单元测试中,我们使用以下内容,
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ServiceTestContextConfiguration.class)
而不是 @Before
,使用 @BeforeEach
并确保所有导入都来自 junit5(包括断言)
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;