即使单元测试成功,Maven构建也会失败

时间:2019-09-29 08:44:17

标签: spring-boot spring-batch

我正在研究一个样本,以演示弹簧批处理的单元测试。在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 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

示例的完整源代码可用here

2 个答案:

答案 0 :(得分:1)

如果使用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

}

请查看What's new部分以获取完整示例。您将在unit testing部分中找到更多详细信息。

答案 1 :(得分:0)

很遗憾,您的测试未执行。由于启动时应用程序上下文失败,因此无法启动。要修复测试,您需要添加遗漏的依赖项(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后,您将获得绿色。