Skip to content

Commit

Permalink
Replace spring.factories with @ContextCustomizerFactories
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Jan 3, 2025
1 parent aa8fb75 commit 28dcfe4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@
* Factory for {@link BatchTestContextCustomizer}.
*
* @author Mahmoud Ben Hassine
* @author Stefano Cordio
* @since 4.1
*/
public class BatchTestContextCustomizerFactory implements ContextCustomizerFactory {

@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
if (AnnotatedElementUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
return new BatchTestContextCustomizer();
}
return null;
return new BatchTestContextCustomizer();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.batch.test.JobRepositoryTestUtils;
import org.springframework.batch.test.JobScopeTestExecutionListener;
import org.springframework.batch.test.StepScopeTestExecutionListener;
import org.springframework.test.context.ContextCustomizerFactories;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension;

Expand Down Expand Up @@ -130,6 +131,7 @@
*
* @author Mahmoud Ben Hassine
* @author Taeik Lim
* @author Stefano Cordio
* @since 4.1
* @see JobLauncherTestUtils
* @see JobRepositoryTestUtils
Expand All @@ -140,6 +142,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@ContextCustomizerFactories(BatchTestContextCustomizerFactory.class)
@TestExecutionListeners(listeners = { StepScopeTestExecutionListener.class, JobScopeTestExecutionListener.class },
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
@ExtendWith(SpringExtension.class)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

/**
* @author Mahmoud Ben Hassine
Expand All @@ -43,7 +42,7 @@ void testCreateContextCustomizer_whenAnnotationIsPresent() {
ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes);

// then
assertNotNull(contextCustomizer);
assertInstanceOf(BatchTestContextCustomizer.class, contextCustomizer);
}

@Test
Expand All @@ -56,7 +55,7 @@ void testCreateContextCustomizer_whenAnnotationIsAbsent() {
ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes);

// then
assertNull(contextCustomizer);
assertInstanceOf(BatchTestContextCustomizer.class, contextCustomizer);
}

@SpringBatchTest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.springframework.batch.test.context;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import static org.junit.jupiter.api.Assertions.assertSame;

/**
* @author Stefano Cordio
*/
@SpringJUnitConfig
@SpringBatchTest
class SpringBatchTestIntegrationTests {

@Autowired
ApplicationContext context;

@Nested
class InnerWithoutSpringBatchTest {

@Autowired
ApplicationContext context;

@Test
void test() {
assertSame(SpringBatchTestIntegrationTests.this.context, context);
}

}

@Nested
@SpringBatchTest
class InnerWithSpringBatchTest {

@Autowired
ApplicationContext context;

@Test
void test() {
assertSame(SpringBatchTestIntegrationTests.this.context, context);
}

}

}

0 comments on commit 28dcfe4

Please sign in to comment.