Skip to content

Commit 822febd

Browse files
committed
GH-173 - Support for @ApplicationModuleTest in JUnit 5 nested test classes.
We now use Spring Test Context's TestContextAnnotationUtils to lookup the @ApplicationModuleTest annotation to eventually bootstrap an ApplicationModules instance. That ensures that we find the annotation on JUnit 5's @nested classes.
1 parent 281da55 commit 822febd

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 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 com.acme.myproject.moduleA;
17+
18+
import org.junit.jupiter.api.Nested;
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.modulith.test.ApplicationModuleTest;
21+
22+
/**
23+
* Integration tests for {@link ApplicationModuleTest}.
24+
*
25+
* @author Oliver Drotbohm
26+
*/
27+
@ApplicationModuleTest(verifyAutomatically = false)
28+
class ApplicationModuleTestIntegrationTests {
29+
30+
@Test // GH-173
31+
void bootstrapsFirstLevelTestMethod() {}
32+
33+
@Nested
34+
class SomeNestedClass {
35+
36+
@Test // GH-173
37+
void bootstrapsSecondLevelMestMethod() {}
38+
}
39+
}

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
2727
import org.springframework.context.ConfigurableApplicationContext;
28-
import org.springframework.core.annotation.AnnotatedElementUtils;
2928
import org.springframework.modulith.core.ApplicationModule;
3029
import org.springframework.test.context.ContextConfigurationAttributes;
3130
import org.springframework.test.context.ContextCustomizer;
3231
import org.springframework.test.context.ContextCustomizerFactory;
3332
import org.springframework.test.context.MergedContextConfiguration;
33+
import org.springframework.test.context.TestContextAnnotationUtils;
3434

3535
/**
3636
* @author Oliver Drotbohm
@@ -45,9 +45,9 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory {
4545
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4646
List<ContextConfigurationAttributes> configAttributes) {
4747

48-
var moduleTest = AnnotatedElementUtils.getMergedAnnotation(testClass, ApplicationModuleTest.class);
48+
var moduleTest = TestContextAnnotationUtils.findAnnotationDescriptor(testClass, ApplicationModuleTest.class);
4949

50-
return moduleTest == null ? null : new ModuleContextCustomizer(testClass);
50+
return moduleTest == null ? null : new ModuleContextCustomizer(moduleTest.getRootDeclaringClass());
5151
}
5252

5353
static class ModuleContextCustomizer implements ContextCustomizer {

0 commit comments

Comments
 (0)