Reputation: 16002
The above two attributes do not work well together.
I want to somehow exclude all the batch processing from my @DataJpaTest. Is there a way to specify a list of classes that should not be scanned in the test?
I have an application class with these attributes.
@SpringBootApplication
@Configuration
@ComponentScan(basePackages = {"com.app.customer", "com.app.framework"})
@EnableGlobalMethodSecurity(
prePostEnabled = true,
securedEnabled = false,
jsr250Enabled = false)
@EnableCaching
public class Application {}
At the same level as the class below I have this:
@Configuration
@EnableBatchProcessing
public class SpringBatchConfiguration {
}
My question is, how in my @DataJpaTest
do I try and exclude classes that involve the batch processing.
@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.
}
}
The logs bear evidence to this.
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]
Upvotes: 2
Views: 898
Reputation: 2817
You can use @EnableAutoConfiguration(exclude=XYZ.class)
to exclude auto configuration class from your tests:
In this way you can disable batch auto configuration by adding @EnableAutoConfiguration
annotation to your test class as :
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(classes = {Application.class})
@EnableAutoConfiguration(exclude = {BatchAutoConfiguration.class})
public class CustomerPlanRepositoryTest {
}
Another way you can use to achieve the same thing is creating a TestApplication
class :
@SpringBootApplication(scanBasePackageClasses = {Application.class}, exclude = {BatchAutoConfiguration.class})
public class TestApplication { }
And use it in your CustomerPlanRepositoryTest
class:
@RunWith(SpringRunner.class)
@DataJpaTest
@SpringBootTest(classes = {TestApplication.class})
public class CustomerPlanRepositoryTest {
}
Or you can use Profile feature, by creating application-test.properties
that will contains :
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration
And annotating your test class with @ActiveProfiles("test")
like this :
@RunWith(SpringRunner.class)
@DataJpaTest
@ImportAutoConfiguration(Application.class)
@ActiveProfiles("test")
public class CustomerPlanRepositoryTest { }
Upvotes: 3