无法自动装配 JobLauncherTestUtils
Could not autowire JobLauncherTestUtils
我正在尝试测试一个简单的 spring 批处理应用程序。
使用 Spring 批处理文档作为指南 (found here),我创建了以下测试 class:
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");
}
}
根据文档 @SpringBatchTest
应该注入 JobLaucherTestUtils
bean。但是,当我 运行 测试时,断言失败了。我还尝试在内部配置 class 中定义 bean,结果相同:
static class TestConfiguration {
@Autowired
@Qualifier("userJob")
private Job userJob;
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
JobLauncherTestUtils utils = new JobLauncherTestUtils();
utils.setJob(userJob);
return utils;
}
}
有什么我想念的吗?可以找到完整的源代码 here.
I am using Spring Batch v4.2.0 and JUnit 5
您正在使用用于 JUnit 4 的 @RunWith(SpringRunner.class)
。对于 JUnit 5 测试,您需要使用 @ExtendWith(SpringExtension.class)
:
@SpringBatchTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
// ...
}
我在使用 Spring Batch 4.1.3 和 JUnit 4.12 时遇到了同样的问题。
用 @SpringBootTest
替换 @SpringBatchTest
解决了问题。
我在 IntelliJ 中看到了同样的错误,并且在我 运行 测试之前花了很长时间来研究原因。它被注入得很好,IntelliJ 错误地告诉我 bean 不存在。
:脸掌:
张贴这个以防有人遇到同样的问题。
我正在尝试测试一个简单的 spring 批处理应用程序。
使用 Spring 批处理文档作为指南 (found here),我创建了以下测试 class:
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");
}
}
根据文档 @SpringBatchTest
应该注入 JobLaucherTestUtils
bean。但是,当我 运行 测试时,断言失败了。我还尝试在内部配置 class 中定义 bean,结果相同:
static class TestConfiguration {
@Autowired
@Qualifier("userJob")
private Job userJob;
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
JobLauncherTestUtils utils = new JobLauncherTestUtils();
utils.setJob(userJob);
return utils;
}
}
有什么我想念的吗?可以找到完整的源代码 here.
I am using Spring Batch v4.2.0 and JUnit 5
您正在使用用于 JUnit 4 的 @RunWith(SpringRunner.class)
。对于 JUnit 5 测试,您需要使用 @ExtendWith(SpringExtension.class)
:
@SpringBatchTest
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = BatchConfig.class)
class BatchConfigTest {
// ...
}
我在使用 Spring Batch 4.1.3 和 JUnit 4.12 时遇到了同样的问题。
用 @SpringBootTest
替换 @SpringBatchTest
解决了问题。
我在 IntelliJ 中看到了同样的错误,并且在我 运行 测试之前花了很长时间来研究原因。它被注入得很好,IntelliJ 错误地告诉我 bean 不存在。
:脸掌:
张贴这个以防有人遇到同样的问题。