Skip to content

Commit a1b6577

Browse files
committed
Polishing BatchAutoConfigurationTests
1. Rename `TestConfiguration` to `TestJpaConfiguration` and remove unnecessary reference. 2. Use `withBean` instead of `withUserConfiguration` to avoid new configuration class. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 4147943 commit a1b6577

File tree

1 file changed

+60
-79
lines changed

1 file changed

+60
-79
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

Lines changed: 60 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
* @author Mahmoud Ben Hassine
108108
* @author Lars Uffmann
109109
* @author Lasse Wulff
110+
* @author Yanming Zhou
110111
*/
111112
@ExtendWith(OutputCaptureExtension.class)
112113
class BatchAutoConfigurationTests {
@@ -117,23 +118,22 @@ class BatchAutoConfigurationTests {
117118

118119
@Test
119120
void testDefaultContext() {
120-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
121-
.run((context) -> {
122-
assertThat(context).hasSingleBean(JobRepository.class);
123-
assertThat(context).hasSingleBean(JobLauncher.class);
124-
assertThat(context).hasSingleBean(JobExplorer.class);
125-
assertThat(context).hasSingleBean(JobRegistry.class);
126-
assertThat(context).hasSingleBean(JobOperator.class);
127-
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
128-
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
129-
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
130-
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
131-
});
121+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
122+
assertThat(context).hasSingleBean(JobRepository.class);
123+
assertThat(context).hasSingleBean(JobLauncher.class);
124+
assertThat(context).hasSingleBean(JobExplorer.class);
125+
assertThat(context).hasSingleBean(JobRegistry.class);
126+
assertThat(context).hasSingleBean(JobOperator.class);
127+
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
128+
.isEqualTo(DatabaseInitializationMode.EMBEDDED);
129+
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
130+
.queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty();
131+
});
132132
}
133133

134134
@Test
135135
void autoconfigurationBacksOffEntirelyIfSpringJdbcAbsent() {
136-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
136+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
137137
.withClassLoader(new FilteredClassLoader(DatabasePopulator.class))
138138
.run((context) -> {
139139
assertThat(context).doesNotHaveBean(JobLauncherApplicationRunner.class);
@@ -257,7 +257,7 @@ void testDisableLaunchesJob() {
257257

258258
@Test
259259
void testDisableSchemaLoader() {
260-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
260+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
261261
.withPropertyValues("spring.datasource.generate-unique-name=true",
262262
"spring.batch.jdbc.initialize-schema:never")
263263
.run((context) -> {
@@ -274,7 +274,7 @@ void testDisableSchemaLoader() {
274274
@Test
275275
void testUsingJpa() {
276276
this.contextRunner
277-
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
277+
.withUserConfiguration(TestJpaConfiguration.class, EmbeddedDataSourceConfiguration.class,
278278
HibernateJpaAutoConfiguration.class)
279279
.run((context) -> {
280280
PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
@@ -292,9 +292,7 @@ void testUsingJpa() {
292292
@Test
293293
@WithPackageResources("custom-schema.sql")
294294
void testRenamePrefix() {
295-
this.contextRunner
296-
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
297-
HibernateJpaAutoConfiguration.class)
295+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
298296
.withPropertyValues("spring.datasource.generate-unique-name=true",
299297
"spring.batch.jdbc.schema:classpath:custom-schema.sql", "spring.batch.jdbc.tablePrefix:PREFIX_")
300298
.run((context) -> {
@@ -313,7 +311,7 @@ void testRenamePrefix() {
313311
@Test
314312
void testCustomizeJpaTransactionManagerUsingProperties() {
315313
this.contextRunner
316-
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
314+
.withUserConfiguration(TestJpaConfiguration.class, EmbeddedDataSourceConfiguration.class,
317315
HibernateJpaAutoConfiguration.class)
318316
.withPropertyValues("spring.transaction.default-timeout:30",
319317
"spring.transaction.rollback-on-commit-failure:true")
@@ -328,7 +326,7 @@ void testCustomizeJpaTransactionManagerUsingProperties() {
328326

329327
@Test
330328
void testCustomizeDataSourceTransactionManagerUsingProperties() {
331-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
329+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
332330
.withPropertyValues("spring.transaction.default-timeout:30",
333331
"spring.transaction.rollback-on-commit-failure:true")
334332
.run((context) -> {
@@ -342,36 +340,32 @@ void testCustomizeDataSourceTransactionManagerUsingProperties() {
342340

343341
@Test
344342
void testBatchDataSource() {
345-
this.contextRunner.withUserConfiguration(TestConfiguration.class, BatchDataSourceConfiguration.class)
346-
.run((context) -> {
347-
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class)
348-
.hasSingleBean(BatchDataSourceScriptDatabaseInitializer.class)
349-
.hasBean("batchDataSource");
350-
DataSource batchDataSource = context.getBean("batchDataSource", DataSource.class);
351-
assertThat(context.getBean(SpringBootBatchConfiguration.class).getDataSource())
352-
.isEqualTo(batchDataSource);
353-
assertThat(context.getBean(BatchDataSourceScriptDatabaseInitializer.class))
354-
.hasFieldOrPropertyWithValue("dataSource", batchDataSource);
355-
});
343+
this.contextRunner.withUserConfiguration(BatchDataSourceConfiguration.class).run((context) -> {
344+
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class)
345+
.hasSingleBean(BatchDataSourceScriptDatabaseInitializer.class)
346+
.hasBean("batchDataSource");
347+
DataSource batchDataSource = context.getBean("batchDataSource", DataSource.class);
348+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getDataSource()).isEqualTo(batchDataSource);
349+
assertThat(context.getBean(BatchDataSourceScriptDatabaseInitializer.class))
350+
.hasFieldOrPropertyWithValue("dataSource", batchDataSource);
351+
});
356352
}
357353

358354
@Test
359355
void testBatchTransactionManager() {
360-
this.contextRunner.withUserConfiguration(TestConfiguration.class, BatchTransactionManagerConfiguration.class)
361-
.run((context) -> {
362-
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class);
363-
PlatformTransactionManager batchTransactionManager = context.getBean("batchTransactionManager",
364-
PlatformTransactionManager.class);
365-
assertThat(context.getBean(SpringBootBatchConfiguration.class).getTransactionManager())
366-
.isEqualTo(batchTransactionManager);
367-
});
356+
this.contextRunner.withUserConfiguration(BatchTransactionManagerConfiguration.class).run((context) -> {
357+
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class);
358+
PlatformTransactionManager batchTransactionManager = context.getBean("batchTransactionManager",
359+
PlatformTransactionManager.class);
360+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getTransactionManager())
361+
.isEqualTo(batchTransactionManager);
362+
});
368363
}
369364

370365
@Test
371366
void testBatchTaskExecutor() {
372367
this.contextRunner
373-
.withUserConfiguration(TestConfiguration.class, BatchTaskExecutorConfiguration.class,
374-
EmbeddedDataSourceConfiguration.class)
368+
.withUserConfiguration(BatchTaskExecutorConfiguration.class, EmbeddedDataSourceConfiguration.class)
375369
.run((context) -> {
376370
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class).hasBean("batchTaskExecutor");
377371
TaskExecutor batchTaskExecutor = context.getBean("batchTaskExecutor", TaskExecutor.class);
@@ -385,22 +379,20 @@ void testBatchTaskExecutor() {
385379

386380
@Test
387381
void jobRepositoryBeansDependOnBatchDataSourceInitializer() {
388-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
389-
.run((context) -> {
390-
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
391-
String[] jobRepositoryNames = beanFactory.getBeanNamesForType(JobRepository.class);
392-
assertThat(jobRepositoryNames).isNotEmpty();
393-
for (String jobRepositoryName : jobRepositoryNames) {
394-
assertThat(beanFactory.getBeanDefinition(jobRepositoryName).getDependsOn())
395-
.contains("batchDataSourceInitializer");
396-
}
397-
});
382+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
383+
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
384+
String[] jobRepositoryNames = beanFactory.getBeanNamesForType(JobRepository.class);
385+
assertThat(jobRepositoryNames).isNotEmpty();
386+
for (String jobRepositoryName : jobRepositoryNames) {
387+
assertThat(beanFactory.getBeanDefinition(jobRepositoryName).getDependsOn())
388+
.contains("batchDataSourceInitializer");
389+
}
390+
});
398391
}
399392

400393
@Test
401394
void jobRepositoryBeansDependOnFlyway() {
402-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
403-
.withUserConfiguration(FlywayAutoConfiguration.class)
395+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class)
404396
.withPropertyValues("spring.batch.initialize-schema=never")
405397
.run((context) -> {
406398
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
@@ -416,8 +408,8 @@ void jobRepositoryBeansDependOnFlyway() {
416408
@Test
417409
@WithResource(name = "db/changelog/db.changelog-master.yaml", content = "databaseChangeLog:")
418410
void jobRepositoryBeansDependOnLiquibase() {
419-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
420-
.withUserConfiguration(LiquibaseAutoConfiguration.class)
411+
this.contextRunner
412+
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, LiquibaseAutoConfiguration.class)
421413
.withPropertyValues("spring.batch.initialize-schema=never")
422414
.run((context) -> {
423415
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
@@ -431,8 +423,7 @@ void jobRepositoryBeansDependOnLiquibase() {
431423

432424
@Test
433425
void whenTheUserDefinesTheirOwnBatchDatabaseInitializerThenTheAutoConfiguredInitializerBacksOff() {
434-
this.contextRunner
435-
.withUserConfiguration(TestConfiguration.class, CustomBatchDatabaseInitializerConfiguration.class)
426+
this.contextRunner.withUserConfiguration(CustomBatchDatabaseInitializerConfiguration.class)
436427
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
437428
DataSourceTransactionManagerAutoConfiguration.class))
438429
.run((context) -> assertThat(context).hasSingleBean(BatchDataSourceScriptDatabaseInitializer.class)
@@ -442,7 +433,7 @@ void whenTheUserDefinesTheirOwnBatchDatabaseInitializerThenTheAutoConfiguredInit
442433

443434
@Test
444435
void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInitializerRemains() {
445-
this.contextRunner.withUserConfiguration(TestConfiguration.class, CustomDatabaseInitializerConfiguration.class)
436+
this.contextRunner.withUserConfiguration(CustomDatabaseInitializerConfiguration.class)
446437
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
447438
DataSourceTransactionManagerAutoConfiguration.class))
448439
.run((context) -> assertThat(context).hasSingleBean(BatchDataSourceScriptDatabaseInitializer.class)
@@ -451,8 +442,9 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
451442

452443
@Test
453444
void conversionServiceCustomizersAreCalled() {
454-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
455-
.withUserConfiguration(ConversionServiceCustomizersConfiguration.class)
445+
this.contextRunner
446+
.withUserConfiguration(EmbeddedDataSourceConfiguration.class,
447+
ConversionServiceCustomizersConfiguration.class)
456448
.run((context) -> {
457449
BatchConversionServiceCustomizer customizer = context.getBean("batchConversionServiceCustomizer",
458450
BatchConversionServiceCustomizer.class);
@@ -501,8 +493,8 @@ void whenTheUserDefinesAJobNameThatDoesNotExistWithRegisteredJobFailsFast() {
501493

502494
@Test
503495
void customExecutionContextSerializerIsUsed() {
504-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
505-
.withUserConfiguration(CustomExecutionContextConfiguration.class)
496+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
497+
.withBean(ExecutionContextSerializer.class, Jackson2ExecutionContextStringSerializer::new)
506498
.run((context) -> {
507499
assertThat(context).hasSingleBean(Jackson2ExecutionContextStringSerializer.class);
508500
assertThat(context.getBean(SpringBootBatchConfiguration.class).getExecutionContextSerializer())
@@ -512,12 +504,11 @@ void customExecutionContextSerializerIsUsed() {
512504

513505
@Test
514506
void defaultExecutionContextSerializerIsUsed() {
515-
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
516-
.run((context) -> {
517-
assertThat(context).doesNotHaveBean(ExecutionContextSerializer.class);
518-
assertThat(context.getBean(SpringBootBatchConfiguration.class).getExecutionContextSerializer())
519-
.isInstanceOf(DefaultExecutionContextSerializer.class);
520-
});
507+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
508+
assertThat(context).doesNotHaveBean(ExecutionContextSerializer.class);
509+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getExecutionContextSerializer())
510+
.isInstanceOf(DefaultExecutionContextSerializer.class);
511+
});
521512
}
522513

523514
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
@@ -595,7 +586,7 @@ static class EmptyConfiguration {
595586
}
596587

597588
@TestAutoConfigurationPackage(City.class)
598-
static class TestConfiguration {
589+
static class TestJpaConfiguration {
599590

600591
}
601592

@@ -873,14 +864,4 @@ BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() {
873864

874865
}
875866

876-
@Configuration(proxyBeanMethods = false)
877-
static class CustomExecutionContextConfiguration {
878-
879-
@Bean
880-
ExecutionContextSerializer executionContextSerializer() {
881-
return new Jackson2ExecutionContextStringSerializer();
882-
}
883-
884-
}
885-
886867
}

0 commit comments

Comments
 (0)