Skip to content

Commit

Permalink
Honor @NestedTestConfiguration semantic in `BatchTestContextCustomi…
Browse files Browse the repository at this point in the history
…zerFactory`
  • Loading branch information
scordio committed Jan 8, 2025
1 parent aa8fb75 commit b188e4b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@

import java.util.List;

import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.TestContextAnnotationUtils;

/**
* 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,
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
if (AnnotatedElementUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
if (TestContextAnnotationUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
return new BatchTestContextCustomizer();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,42 @@
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
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.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* @author Mahmoud Ben Hassine
* @author Stefano Cordio
*/
class BatchTestContextCustomizerFactoryTests {

private final BatchTestContextCustomizerFactory factory = new BatchTestContextCustomizerFactory();

@Test
void testCreateContextCustomizer_whenAnnotationIsPresent() {
@ParameterizedTest
@ValueSource(classes = { MyJobTest.class, MyJobTest.MyNestedTest.class, })
void testCreateContextCustomizer_whenAnnotationIsPresent(Class<?> testClass) {
// given
Class<MyJobTest> testClass = MyJobTest.class;
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();

// when
ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes);

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

@Test
void testCreateContextCustomizer_whenAnnotationIsAbsent() {
// given
Class<MyOtherJobTest> testClass = MyOtherJobTest.class;
Class<?> testClass = MyOtherJobTest.class;
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();

// when
Expand All @@ -62,6 +66,11 @@ void testCreateContextCustomizer_whenAnnotationIsAbsent() {
@SpringBatchTest
private static class MyJobTest {

@Nested
class MyNestedTest {

}

}

private static class MyOtherJobTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.springframework.batch.test.context;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.JobRepositoryTestUtils;
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.assertNotNull;
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);
assertNotNull(context.getBean(JobLauncherTestUtils.class));
assertNotNull(context.getBean(JobRepositoryTestUtils.class));
}

}

@Nested
@SpringBatchTest
class InnerWithSpringBatchTest {

@Autowired
ApplicationContext context;

@Test
void test() {
assertSame(SpringBatchTestIntegrationTests.this.context, context);
assertNotNull(context.getBean(JobLauncherTestUtils.class));
assertNotNull(context.getBean(JobRepositoryTestUtils.class));
}

}

}

0 comments on commit b188e4b

Please sign in to comment.