Skip to content

Commit a233c08

Browse files
committed
discover @AutoConfigureEmbeddedDatabase on enclosing class for nested test class
1 parent aadd1dd commit a233c08

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

embedded-database-spring-test/src/main/java/io/zonky/test/db/util/AnnotationUtils.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,43 @@
1717
package io.zonky.test.db.util;
1818

1919
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
20-
import io.zonky.test.db.AutoConfigureEmbeddedDatabase.Replace;
21-
import io.zonky.test.db.AutoConfigureEmbeddedDatabases;
2220
import org.springframework.core.annotation.AnnotatedElementUtils;
21+
import org.springframework.util.ClassUtils;
2322

2423
import java.util.LinkedHashSet;
2524
import java.util.Set;
2625
import java.util.concurrent.ConcurrentHashMap;
2726
import java.util.function.Function;
2827
import java.util.function.Predicate;
2928

29+
import static io.zonky.test.db.util.ReflectionUtils.invokeStaticMethod;
3030
import static java.util.stream.Collectors.toCollection;
3131

3232
public class AnnotationUtils {
3333

34+
private static final Class<?> testContextAnnotationUtilsClass;
35+
36+
static {
37+
Class<?> targetClass;
38+
try {
39+
ClassLoader classLoader = AnnotationUtils.class.getClassLoader();
40+
targetClass = ClassUtils.forName("org.springframework.test.context.TestContextAnnotationUtils", classLoader);
41+
} catch (ClassNotFoundException e) {
42+
targetClass = null;
43+
}
44+
testContextAnnotationUtilsClass = targetClass;
45+
}
46+
3447
private AnnotationUtils() {}
3548

3649
public static Set<AutoConfigureEmbeddedDatabase> getDatabaseAnnotations(Class<?> annotatedElement) {
37-
Set<AutoConfigureEmbeddedDatabase> annotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(
38-
annotatedElement, AutoConfigureEmbeddedDatabase.class, AutoConfigureEmbeddedDatabases.class);
50+
Set<AutoConfigureEmbeddedDatabase> annotations;
51+
52+
if (testContextAnnotationUtilsClass != null) {
53+
annotations = invokeStaticMethod(testContextAnnotationUtilsClass, "getMergedRepeatableAnnotations", annotatedElement, AutoConfigureEmbeddedDatabase.class);
54+
} else {
55+
annotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, AutoConfigureEmbeddedDatabase.class);
56+
}
3957

4058
return annotations.stream()
4159
.filter(distinctByKey(AutoConfigureEmbeddedDatabase::beanName))

0 commit comments

Comments
 (0)