Skip to content

Commit ed4a318

Browse files
Aleksandr Arshavskiyfmbenhassine
authored andcommitted
Disable BatchTestContextCustomizer after AOT processing
Issue #4286
1 parent 46c8ebb commit ed4a318

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/context/BatchTestContextCustomizer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.test.context;
1717

18+
import org.springframework.aot.AotDetector;
1819
import org.springframework.batch.test.JobLauncherTestUtils;
1920
import org.springframework.batch.test.JobRepositoryTestUtils;
2021
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -30,7 +31,7 @@
3031
* ({@link JobLauncherTestUtils} and {@link JobRepositoryTestUtils}) as beans in the test
3132
* context.
3233
*
33-
* @author Mahmoud Ben Hassine
34+
* @author Mahmoud Ben Hassine, Alexander Arshavskiy
3435
* @since 4.1
3536
*/
3637
public class BatchTestContextCustomizer implements ContextCustomizer {
@@ -43,6 +44,10 @@ public class BatchTestContextCustomizer implements ContextCustomizer {
4344

4445
@Override
4546
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
47+
if (AotDetector.useGeneratedArtifacts()) {
48+
return;
49+
}
50+
4651
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
4752
Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory,
4853
"The bean factory must be an instance of BeanDefinitionRegistry");

spring-batch-test/src/test/java/org/springframework/batch/test/context/BatchTestContextCustomizerTests.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,25 +15,33 @@
1515
*/
1616
package org.springframework.batch.test.context;
1717

18+
import org.junit.jupiter.api.AfterEach;
1819
import org.junit.jupiter.api.Test;
1920
import org.mockito.Mockito;
2021

2122
import org.springframework.context.ConfigurableApplicationContext;
2223
import org.springframework.context.support.GenericApplicationContext;
24+
import org.springframework.core.SpringProperties;
2325
import org.springframework.test.context.MergedContextConfiguration;
2426

2527
import static org.hamcrest.CoreMatchers.containsString;
2628
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.junit.jupiter.api.Assertions.assertFalse;
2730
import static org.junit.jupiter.api.Assertions.assertThrows;
2831
import static org.junit.jupiter.api.Assertions.assertTrue;
2932

3033
/**
31-
* @author Mahmoud Ben Hassine
34+
* @author Mahmoud Ben Hassine, Alexander Arshavskiy
3235
*/
3336
class BatchTestContextCustomizerTests {
3437

3538
private final BatchTestContextCustomizer contextCustomizer = new BatchTestContextCustomizer();
3639

40+
@AfterEach
41+
void removeSystemProperty() {
42+
SpringProperties.setProperty("spring.aot.enabled", null);
43+
}
44+
3745
@Test
3846
void testCustomizeContext() {
3947
// given
@@ -64,4 +72,20 @@ void testCustomizeContext_whenBeanFactoryIsNotAnInstanceOfBeanDefinitionRegistry
6472
containsString("The bean factory must be an instance of BeanDefinitionRegistry"));
6573
}
6674

75+
@Test
76+
void testCustomizeContext_whenUsingAotGeneratedArtifactsBatchTestContextIsNotRegistered() {
77+
// given
78+
SpringProperties.setProperty("spring.aot.enabled", "true");
79+
ConfigurableApplicationContext context = new GenericApplicationContext();
80+
MergedContextConfiguration mergedConfig = Mockito.mock(MergedContextConfiguration.class);
81+
82+
// when
83+
this.contextCustomizer.customizeContext(context, mergedConfig);
84+
85+
// then
86+
assertFalse(context.containsBean("jobLauncherTestUtils"));
87+
assertFalse(context.containsBean("jobRepositoryTestUtils"));
88+
assertFalse(context.containsBean("batchTestContextBeanPostProcessor"));
89+
}
90+
6791
}

0 commit comments

Comments
 (0)