spring 中的 @EnableBatchProcessing 和 @DataJpaTest 使用休眠启动应用程序

@EnableBatchProcessing and @DataJpaTest in spring boot application with hibernate

以上两个属性不能很好地协同工作。

我想以某种方式从我的@DataJpaTest 中排除所有批处理。 有没有办法指定在测试中不应扫描的 classes 列表?

我有一个具有这些属性的应用程序 class。

@SpringBootApplication
@Configuration
@ComponentScan(basePackages = {"com.app.customer", "com.app.framework"})
@EnableGlobalMethodSecurity(
        prePostEnabled = true,
        securedEnabled = false,
        jsr250Enabled = false)
@EnableCaching
public class Application {}

与下面的 class 处于同一级别,我有这个:

@Configuration
@EnableBatchProcessing
public class SpringBatchConfiguration {
}

我的问题是,如何在我的 @DataJpaTest 中尝试排除涉及批处理的 classes。

@RunWith(SpringRunner.class)
@DataJpaTest
@ImportAutoConfiguration(Application.class)
public class CustomerPlanRepositoryTest {

   @Test
   public void testFindDefaultCustomerPlanForCustomerId() {
    // these tests run, but they are broken, as the insert does not work 
        correctly.
    // there is no transaction manager, and the tests fail.
   }
}

日志证明了这一点。

2020-10-30 14:39:34,763 WARN  [main] org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer: No transaction manager was provided, using a DataSourceTransactionManager
2020-10-30 14:39:34,771 INFO  [main] org.springframework.batch.core.repository.support.JobRepositoryFactoryBean: No database type set, using meta data indicating: H2
2020-10-30 14:39:34,788 INFO  [main] org.springframework.batch.core.launch.support.SimpleJobLauncher: No TaskExecutor has been set, defaulting to synchronous executor.
2020-10-30 14:39:34,791 INFO  [main] org.springframework.test.context.transaction.TransactionContext: Began transaction (1) for test context [DefaultTestContext@72cde7cc testClass = CustomerPlanRepositoryTest, testInstance = com.payzilch.customer.repositories.CustomerPlanRepositoryTest@5f6b899d, testMethod = testFindDefaultCustomerPlanForCustomerId@CustomerPlanRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@5fd4f8f5 testClass = CustomerPlanRepositoryTest, locations = '{}', classes = '{class com.payzilch.customer.PayZilchCustomerServiceApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@696da30b key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration, com.payzilch.customer.PayZilchCustomerServiceApplication]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@52bf72b5, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@dd8ba08, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@76fea90b, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@2f9f7dcf, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@c6d4beb8, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@31304f14, org.springframework.boot.test.context.SpringBootTestArgs@1], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@6c1a08f2]; rollback [true]

您可以使用 @EnableAutoConfiguration(exclude=XYZ.class) 从测试中排除自动配置 class:

通过这种方式,您可以通过向测试 class 添加 @EnableAutoConfiguration 注释来禁用批量自动配置,如:

@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(classes = {Application.class})
@EnableAutoConfiguration(exclude = {BatchAutoConfiguration.class})
public class CustomerPlanRepositoryTest {

}

您可以用来实现相同目的的另一种方法是创建 TestApplication class :

@SpringBootApplication(scanBasePackageClasses = {Application.class}, exclude = {BatchAutoConfiguration.class})
public class TestApplication { }

并在您的 CustomerPlanRepositoryTest class:

中使用它
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(classes = {TestApplication.class})
public class CustomerPlanRepositoryTest {
}

或者您可以使用配置文件功能,创建 application-test.properties 将包含:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration

并像这样用 @ActiveProfiles("test") 注释你的测试 class :

@RunWith(SpringRunner.class)
@DataJpaTest
@ImportAutoConfiguration(Application.class)
@ActiveProfiles("test")
public class CustomerPlanRepositoryTest { }