Skip to content

Commit e366dcb

Browse files
scordiofmbenhassine
authored andcommitted
Honor @NestedTestConfiguration semantic in BatchTestContextCustomizerFactory
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
1 parent 6701606 commit e366dcb

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2025 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.
@@ -17,23 +17,25 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.core.annotation.AnnotatedElementUtils;
20+
import org.springframework.lang.Nullable;
2121
import org.springframework.test.context.ContextConfigurationAttributes;
2222
import org.springframework.test.context.ContextCustomizer;
2323
import org.springframework.test.context.ContextCustomizerFactory;
24+
import org.springframework.test.context.TestContextAnnotationUtils;
2425

2526
/**
2627
* Factory for {@link BatchTestContextCustomizer}.
2728
*
2829
* @author Mahmoud Ben Hassine
30+
* @author Stefano Cordio
2931
* @since 4.1
3032
*/
3133
public class BatchTestContextCustomizerFactory implements ContextCustomizerFactory {
3234

3335
@Override
34-
public ContextCustomizer createContextCustomizer(Class<?> testClass,
36+
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
3537
List<ContextConfigurationAttributes> configAttributes) {
36-
if (AnnotatedElementUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
38+
if (TestContextAnnotationUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
3739
return new BatchTestContextCustomizer();
3840
}
3941
return null;

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

+16-7
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-2025 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.
@@ -18,38 +18,42 @@
1818
import java.util.Collections;
1919
import java.util.List;
2020

21+
import org.junit.jupiter.api.Nested;
2122
import org.junit.jupiter.api.Test;
2223

24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2326
import org.springframework.test.context.ContextConfigurationAttributes;
2427
import org.springframework.test.context.ContextCustomizer;
2528

26-
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2730
import static org.junit.jupiter.api.Assertions.assertNull;
2831

2932
/**
3033
* @author Mahmoud Ben Hassine
34+
* @author Stefano Cordio
3135
*/
3236
class BatchTestContextCustomizerFactoryTests {
3337

3438
private final BatchTestContextCustomizerFactory factory = new BatchTestContextCustomizerFactory();
3539

36-
@Test
37-
void testCreateContextCustomizer_whenAnnotationIsPresent() {
40+
@ParameterizedTest
41+
@ValueSource(classes = { MyJobTest.class, MyJobTest.MyNestedTest.class })
42+
void testCreateContextCustomizer_whenAnnotationIsPresent(Class<?> testClass) {
3843
// given
39-
Class<MyJobTest> testClass = MyJobTest.class;
4044
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();
4145

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

4549
// then
46-
assertNotNull(contextCustomizer);
50+
assertInstanceOf(BatchTestContextCustomizer.class, contextCustomizer);
4751
}
4852

4953
@Test
5054
void testCreateContextCustomizer_whenAnnotationIsAbsent() {
5155
// given
52-
Class<MyOtherJobTest> testClass = MyOtherJobTest.class;
56+
Class<?> testClass = MyOtherJobTest.class;
5357
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();
5458

5559
// when
@@ -62,6 +66,11 @@ void testCreateContextCustomizer_whenAnnotationIsAbsent() {
6266
@SpringBatchTest
6367
private static class MyJobTest {
6468

69+
@Nested
70+
class MyNestedTest {
71+
72+
}
73+
6574
}
6675

6776
private static class MyOtherJobTest {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.test.context;
17+
18+
import org.junit.jupiter.api.Nested;
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.batch.test.JobLauncherTestUtils;
21+
import org.springframework.batch.test.JobRepositoryTestUtils;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.ApplicationContext;
24+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
25+
26+
import static org.junit.jupiter.api.Assertions.assertNotNull;
27+
import static org.junit.jupiter.api.Assertions.assertSame;
28+
29+
/**
30+
* @author Stefano Cordio
31+
*/
32+
@SpringJUnitConfig
33+
@SpringBatchTest
34+
class SpringBatchTestIntegrationTests {
35+
36+
@Autowired
37+
ApplicationContext context;
38+
39+
@Nested
40+
class InnerWithoutSpringBatchTest {
41+
42+
@Autowired
43+
ApplicationContext context;
44+
45+
@Test
46+
void test() {
47+
assertSame(SpringBatchTestIntegrationTests.this.context, context);
48+
assertNotNull(context.getBean(JobLauncherTestUtils.class));
49+
assertNotNull(context.getBean(JobRepositoryTestUtils.class));
50+
}
51+
52+
}
53+
54+
@Nested
55+
@SpringBatchTest
56+
class InnerWithSpringBatchTest {
57+
58+
@Autowired
59+
ApplicationContext context;
60+
61+
@Test
62+
void test() {
63+
assertSame(SpringBatchTestIntegrationTests.this.context, context);
64+
assertNotNull(context.getBean(JobLauncherTestUtils.class));
65+
assertNotNull(context.getBean(JobRepositoryTestUtils.class));
66+
}
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)