abi*_*ter 5 java spring-boot junit5
我正在尝试为我的应用程序编写 junit 测试用例。使用 Junit 5 后,我无法使用 @ContextConfiguration 创建必要的 bean。
它没有抛出任何错误。但是在测试类中自动装配 bean 时,我得到了空值
我添加了以下依赖项
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"
Run Code Online (Sandbox Code Playgroud)
和我的代码
@ContextConfiguration(classes = ServiceTestContextConfiguration.class)
public class ApplicationConfigTest {
@Autowired
private ApplicationServiceConfiguration
Run Code Online (Sandbox Code Playgroud)
应用服务配置;
@ContextConfiguration
public class ServiceTestContextConfiguration {
@Bean
public ApplicationServiceConfiguration applicationServiceConfiguration() {
return new ApplicationServiceConfiguration();
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 spring-boot 2。
可能我们混合了 junit4 和 junit5 的导入。我们用@Configuration( org.springframework.context.annotation.Configuration;)来注释配置类。
在主要单元测试中,我们使用以下内容,
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ServiceTestContextConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
而不是@Before,使用@BeforeEach并确保所有导入均来自 junit5 (包括断言)
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2927 次 |
| 最近记录: |