Mat*_*ene 3 junit spring spring-batch
我正在尝试测试一个简单的 spring 批处理应用程序。
使用 Spring Batch 文档作为指南(在这里找到),我创建了以下测试类:
import org.junit.runner.RunWith;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
void userStep() {
assertNotNull(jobLauncherTestUtils, "jobLauncherTestUtils should not be null");
}
}
Run Code Online (Sandbox Code Playgroud)
根据文档@SpringBatchTest应该注入JobLaucherTestUtilsbean。但是,当我运行测试时,断言失败。我还尝试在内部配置类中定义 bean 并得到相同的结果:
static class TestConfiguration {
@Autowired
@Qualifier("userJob")
private Job userJob;
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
JobLauncherTestUtils utils = new JobLauncherTestUtils();
utils.setJob(userJob);
return utils;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?完整的源代码可以在这里找到。
我正在使用 Spring Batch v4.2.0 和 JUnit 5
您正在使用@RunWith(SpringRunner.class)which 用于 JUnit 4。您需要@ExtendWith(SpringExtension.class)用于 JUnit 5 测试:
@SpringBatchTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2721 次 |
| 最近记录: |