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.
This commit is contained in:
Oliver Drotbohm
2023-03-27 17:51:15 +02:00
parent 281da55eac
commit 822febd00e
2 changed files with 42 additions and 3 deletions

View File

@@ -25,12 +25,12 @@ import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.modulith.core.ApplicationModule;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils;
/**
* @author Oliver Drotbohm
@@ -45,9 +45,9 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory {
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
var moduleTest = AnnotatedElementUtils.getMergedAnnotation(testClass, ApplicationModuleTest.class);
var moduleTest = TestContextAnnotationUtils.findAnnotationDescriptor(testClass, ApplicationModuleTest.class);
return moduleTest == null ? null : new ModuleContextCustomizer(testClass);
return moduleTest == null ? null : new ModuleContextCustomizer(moduleTest.getRootDeclaringClass());
}
static class ModuleContextCustomizer implements ContextCustomizer {