Skip to content

Commit 55c8c3e

Browse files
committed
Group annotations by dependency and print a warning if the dependency is missing
1 parent 14c95f8 commit 55c8c3e

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/jupiter/JupiterConfigProvider.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,35 @@ public void onLoad(NativeImageConfiguration config) {
7777
@Override
7878
public void onTestClassRegistered(Class<?> testClass, NativeImageConfiguration registry) {
7979
/* Provide support for various annotations */
80-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, TestMethodOrder.class, TestMethodOrder::value);
81-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, ArgumentsSource.class, ArgumentsSource::value);
82-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, ExtendWith.class, ExtendWith::value);
83-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, DisplayNameGeneration.class, DisplayNameGeneration::value);
84-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, IndicativeSentencesGeneration.class, IndicativeSentencesGeneration::generator);
85-
AnnotationUtils.forEachAnnotatedMethodParameter(testClass, ConvertWith.class, annotation -> registry.registerAllClassMembersForReflection(annotation.value()));
86-
AnnotationUtils.forEachAnnotatedMethodParameter(testClass, AggregateWith.class, annotation -> registry.registerAllClassMembersForReflection(annotation.value()));
87-
AnnotationUtils.forEachAnnotatedMethod(testClass, EnumSource.class, (m, annotation) -> handleEnumSource(m, annotation, registry));
88-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, MethodSource.class, JupiterConfigProvider::handleMethodSource);
89-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, EnabledIf.class, JupiterConfigProvider::handleEnabledIf);
90-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, DisabledIf.class, JupiterConfigProvider::handleDisabledIf);
9180

81+
/* Annotations from org.junit.jupiter.api */
9282
try {
93-
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, FieldSource.class, JupiterConfigProvider::handleFieldSource);
83+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, TestMethodOrder.class, TestMethodOrder::value);
84+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, ExtendWith.class, ExtendWith::value);
85+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, DisplayNameGeneration.class, DisplayNameGeneration::value);
86+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, IndicativeSentencesGeneration.class, IndicativeSentencesGeneration::generator);
87+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, EnabledIf.class, JupiterConfigProvider::handleEnabledIf);
88+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, DisabledIf.class, JupiterConfigProvider::handleDisabledIf);
9489
} catch (NoClassDefFoundError e) {
95-
// if users use JUnit version older than 5.11, FieldSource class won't be available
90+
AnnotationUtils.printMissingAnnotationsWarning("org.junit.jupiter.api", List.of("TestMethodOrder", "ExtendWith", "DisplayNameGeneration", "IndicativeSentencesGeneration", "EnabledIf", "DisabledIf"));
91+
}
92+
93+
/* Annotations from org.junit.jupiter.params */
94+
try {
95+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, ArgumentsSource.class, ArgumentsSource::value);
96+
AnnotationUtils.forEachAnnotatedMethodParameter(testClass, ConvertWith.class, annotation -> registry.registerAllClassMembersForReflection(annotation.value()));
97+
AnnotationUtils.forEachAnnotatedMethodParameter(testClass, AggregateWith.class, annotation -> registry.registerAllClassMembersForReflection(annotation.value()));
98+
AnnotationUtils.forEachAnnotatedMethod(testClass, EnumSource.class, (m, annotation) -> handleEnumSource(m, annotation, registry));
99+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, MethodSource.class, JupiterConfigProvider::handleMethodSource);
100+
101+
// special case because the class might not be available because the annotation was introduced in JUnit 5.13
102+
try {
103+
AnnotationUtils.registerClassesFromAnnotationForReflection(testClass, registry, FieldSource.class, JupiterConfigProvider::handleFieldSource);
104+
} catch (NoClassDefFoundError e) {
105+
System.out.println("[junit-platform-native] Cannot register @FieldSource annotation from org.junit.jupiter.params. Please verify that you have this dependency (with version greater than JUnit 5.13) if you want to use this annotation.");
106+
}
107+
} catch (NoClassDefFoundError e) {
108+
AnnotationUtils.printMissingAnnotationsWarning("org.junit.jupiter.params", List.of("ArgumentsSource", "ConvertWith", "AggregateWith", "EnumSource", "MethodSource", "FieldSource"));
96109
}
97110

98111
}

common/junit-platform-native/src/main/java/org/graalvm/junit/platform/config/util/AnnotationUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,12 @@ public static <T extends Annotation> void registerClassesFromAnnotationForReflec
125125
config.registerAllClassMembersForReflection(reflectivelyAccessedClass);
126126
});
127127
}
128+
129+
public static void printMissingAnnotationsWarning(String packageName, List<String> annotations) {
130+
System.out.printf("[junit-platform-native] Cannot register annotations %s from %s. " +
131+
"Please verify that you have dependency that includes %s if you want to use these annotations.",
132+
annotations,
133+
packageName,
134+
packageName);
135+
}
128136
}

0 commit comments

Comments
 (0)