Honor @NestedTestConfiguration semantic in BatchTestContextCustomizerFactory
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
This commit is contained in:
committed by
Mahmoud Ben Hassine
parent
6701606f68
commit
e366dcb197
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2018 the original author or authors.
|
* Copyright 2018-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -17,23 +17,25 @@ package org.springframework.batch.test.context;
|
|||||||
|
|
||||||
import java.util.List;
|
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.ContextConfigurationAttributes;
|
||||||
import org.springframework.test.context.ContextCustomizer;
|
import org.springframework.test.context.ContextCustomizer;
|
||||||
import org.springframework.test.context.ContextCustomizerFactory;
|
import org.springframework.test.context.ContextCustomizerFactory;
|
||||||
|
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory for {@link BatchTestContextCustomizer}.
|
* Factory for {@link BatchTestContextCustomizer}.
|
||||||
*
|
*
|
||||||
* @author Mahmoud Ben Hassine
|
* @author Mahmoud Ben Hassine
|
||||||
|
* @author Stefano Cordio
|
||||||
* @since 4.1
|
* @since 4.1
|
||||||
*/
|
*/
|
||||||
public class BatchTestContextCustomizerFactory implements ContextCustomizerFactory {
|
public class BatchTestContextCustomizerFactory implements ContextCustomizerFactory {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
|
||||||
List<ContextConfigurationAttributes> configAttributes) {
|
List<ContextConfigurationAttributes> configAttributes) {
|
||||||
if (AnnotatedElementUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
|
if (TestContextAnnotationUtils.hasAnnotation(testClass, SpringBatchTest.class)) {
|
||||||
return new BatchTestContextCustomizer();
|
return new BatchTestContextCustomizer();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2018-2022 the original author or authors.
|
* Copyright 2018-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -18,38 +18,42 @@ package org.springframework.batch.test.context;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
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.ContextConfigurationAttributes;
|
||||||
import org.springframework.test.context.ContextCustomizer;
|
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;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Mahmoud Ben Hassine
|
* @author Mahmoud Ben Hassine
|
||||||
|
* @author Stefano Cordio
|
||||||
*/
|
*/
|
||||||
class BatchTestContextCustomizerFactoryTests {
|
class BatchTestContextCustomizerFactoryTests {
|
||||||
|
|
||||||
private final BatchTestContextCustomizerFactory factory = new BatchTestContextCustomizerFactory();
|
private final BatchTestContextCustomizerFactory factory = new BatchTestContextCustomizerFactory();
|
||||||
|
|
||||||
@Test
|
@ParameterizedTest
|
||||||
void testCreateContextCustomizer_whenAnnotationIsPresent() {
|
@ValueSource(classes = { MyJobTest.class, MyJobTest.MyNestedTest.class })
|
||||||
|
void testCreateContextCustomizer_whenAnnotationIsPresent(Class<?> testClass) {
|
||||||
// given
|
// given
|
||||||
Class<MyJobTest> testClass = MyJobTest.class;
|
|
||||||
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();
|
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes);
|
ContextCustomizer contextCustomizer = this.factory.createContextCustomizer(testClass, configAttributes);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertNotNull(contextCustomizer);
|
assertInstanceOf(BatchTestContextCustomizer.class, contextCustomizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCreateContextCustomizer_whenAnnotationIsAbsent() {
|
void testCreateContextCustomizer_whenAnnotationIsAbsent() {
|
||||||
// given
|
// given
|
||||||
Class<MyOtherJobTest> testClass = MyOtherJobTest.class;
|
Class<?> testClass = MyOtherJobTest.class;
|
||||||
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();
|
List<ContextConfigurationAttributes> configAttributes = Collections.emptyList();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
@@ -62,6 +66,11 @@ class BatchTestContextCustomizerFactoryTests {
|
|||||||
@SpringBatchTest
|
@SpringBatchTest
|
||||||
private static class MyJobTest {
|
private static class MyJobTest {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
class MyNestedTest {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class MyOtherJobTest {
|
private static class MyOtherJobTest {
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2025 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user