Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.batch.test.context;

import org.springframework.aot.AotDetector;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
Expand All @@ -30,7 +31,7 @@
* ({@link JobLauncherTestUtils} and {@link JobRepositoryTestUtils}) as beans in the test
* context.
*
* @author Mahmoud Ben Hassine
* @author Mahmoud Ben Hassine, Alexander Arshavskiy
* @since 4.1
*/
public class BatchTestContextCustomizer implements ContextCustomizer {
Expand All @@ -43,6 +44,11 @@ public class BatchTestContextCustomizer implements ContextCustomizer {

@Override
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {

if (AotDetector.useGeneratedArtifacts()) {
return;
}

ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory,
"The bean factory must be an instance of BeanDefinitionRegistry");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@
*/
package org.springframework.batch.test.context;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.SpringProperties;
import org.springframework.test.context.MergedContextConfiguration;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author Mahmoud Ben Hassine
* @author Mahmoud Ben Hassine, Alexander Arshavskiy
*/
class BatchTestContextCustomizerTests {

private final BatchTestContextCustomizer contextCustomizer = new BatchTestContextCustomizer();

@AfterEach
void removeSystemProperty() {
SpringProperties.setProperty("spring.aot.enabled", null);
}

@Test
void testCustomizeContext() {
// given
Expand Down Expand Up @@ -64,4 +72,20 @@ void testCustomizeContext_whenBeanFactoryIsNotAnInstanceOfBeanDefinitionRegistry
containsString("The bean factory must be an instance of BeanDefinitionRegistry"));
}

@Test
void testCustomizeContext_whenUsingAotGeneratedArtifactsBatchTestContextIsNotRegistered() {
// given
SpringProperties.setProperty("spring.aot.enabled", "true");
ConfigurableApplicationContext context = new GenericApplicationContext();
MergedContextConfiguration mergedConfig = Mockito.mock(MergedContextConfiguration.class);

// when
this.contextCustomizer.customizeContext(context, mergedConfig);

// then
assertFalse(context.containsBean("jobLauncherTestUtils"));
assertFalse(context.containsBean("jobRepositoryTestUtils"));
assertFalse(context.containsBean("batchTestContextBeanPostProcessor"));
}

}