Spring boot 即使单元测试成功,Maven构建也会失败
我正在制作一个示例来演示SpringBatch的单元测试。在maven构建期间,我可以看到单元测试成功执行,但构建仍然失败,错误如下 批测试已成功执行。下面是输出Spring boot 即使单元测试成功,Maven构建也会失败,spring-boot,spring-batch,Spring Boot,Spring Batch,我正在制作一个示例来演示SpringBatch的单元测试。在maven构建期间,我可以看到单元测试成功执行,但构建仍然失败,错误如下 批测试已成功执行。下面是输出 2019-09-29 16:30:43.276 INFO 8065 --- [ main] o.s.b.a.b.JobLauncherCommandLineRunner : Running default command line with: [] 2019-09-29 16:30:43.402 INFO
2019-09-29 16:30:43.276 INFO 8065 --- [ main] o.s.b.a.b.JobLauncherCommandLineRunner : Running default command line with: []
2019-09-29 16:30:43.402 INFO 8065 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=transactionProcessingJob]] launched with the following parameters: [{}]
2019-09-29 16:30:43.434 INFO 8065 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [transactionImportStep]
Hibernate: call next value for transaction_entry_seq
Hibernate: call next value for transaction_entry_seq
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
Hibernate: insert into transaction_entry (account_no, amount, transaction_date, transaction_type, id) values (?, ?, ?, ?, ?)
2019-09-29 16:30:43.696 INFO 8065 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=transactionProcessingJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
执行单元文本后,生成失败,出现以下错误
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.batch.test.JobLauncherTestUtils' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1658) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1217) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1171) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE] ... 31 common frames omitted
示例的完整源代码可用很遗憾,您的测试没有执行。它不会启动,因为启动时应用程序上下文失败。要修复测试,您需要添加缺少的依赖项JobLauncherTestUtils。为此,我建议添加测试上下文:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TransactionBatchApplication.class, TransactionBatchTests.TestConfig.class})
//@Transactional
@TestPropertySource(properties = {"spring.batch.job.enabled=false"})
public class TransactionBatchTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private TransactionEntryRepository transactionEntryRepository;
@Test
public void testJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
assertThat("COMPLETED").isEqualTo(jobExecution.getExitStatus().getExitCode());
assertThat(transactionEntryRepository.count()).isEqualTo(18);
}
@Configuration
static class TestConfig {
@Bean
JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
}
}
我还禁用了@Transactional,因为:
java.lang.IllegalStateException: Existing transaction detected in JobRepository. Please fix this and try again (e.g. remove @Transactional annotations from client).
我建议更改断言中的值,实际值和预期值:
assertThat(transactionEntryRepository.count()).isEqualTo(10);
然后检查10是否是有效的预期值,因为实际值是18,所以不确定哪一个是正确的
所以,将期望值更改为18后,您将得到绿色 如果使用Spring Batch v4.1+,可以在测试类上添加@SpringBatchTest注释,它会自动将JobLauncherTestUtils bean添加到测试上下文中:
@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = {JobConfiguration.class})
public class JobTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
// use jobLauncherTestUtils in test methods
}
请查看该部分以获取完整的示例。您将在该部分中找到更多详细信息。作业现在正在处理更改,但预期值18是错误的,它是9,而不是“transactions.csv”中的行数。我查看了日志,发现作业运行了两次。使用“spring.batch.job.enabled=false”属性,批处理没有运行两次,并且测试通过了正确的预期值9。谢谢你给我指出了正确的方向。刚刚添加了对test@TestPropertySourceproperties={spring.batch.job.enabled=false}的修复。但是您发现了这一点。在SpringBatch v4.0和以前的版本中,手动将JobLauncherTestUtils bean添加到测试上下文是一种方法。所以这是一个正确的答案。在v4.1中,有一个名为@SpringBatchTest的新注释,它添加了一个自动查看我的答案的示例。谢谢。成功了。因此,我最初失败的根本原因是让作业再次运行。将“spring.batch.job.enabled=false”属性添加到我的测试资源中可以解决我的问题。