native-build-tools icon indicating copy to clipboard operation
native-build-tools copied to clipboard

JUnit 5 native tests fail with NoSuchElementException: No value present

Open graemerocher opened this issue 3 years ago • 8 comments

I have been enabling native testing on the micronaut-test project suite and the tests fails with:

Failures (1):
  JUnit Jupiter
    => org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to execute tests
       org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:113)
       org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
       org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
       org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
       org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
       [...]
     Caused by: org.junit.platform.commons.JUnitException: Error executing tests for engine junit-jupiter
       org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:57)
       org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
       [...]
     Caused by: java.util.NoSuchElementException: No value present
       java.util.Optional.get(Optional.java:143)
       org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations(AnnotationUtils.java:354)
       org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations(AnnotationUtils.java:320)
       org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations(AnnotationUtils.java:364)
       org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations(AnnotationUtils.java:320)
       [...]

Is this is a known issue?

graemerocher avatar Nov 30 '21 15:11 graemerocher

Seems reflection is being used in AnnotationUtil:

Method method = ReflectionUtils.tryToGetMethod(candidateAnnotationType, "value").toOptional().get();

@sbrannen do all JUnit 5 annotations need registering for reflection?

graemerocher avatar Nov 30 '21 15:11 graemerocher

Full trace https://github.com/micronaut-projects/micronaut-test/runs/4369136089?check_suite_focus=true

graemerocher avatar Nov 30 '21 15:11 graemerocher

Worked around with https://github.com/micronaut-projects/micronaut-test/pull/490/commits/db88c001be7b5a70e4f7382968e6e11ab3a2c191

graemerocher avatar Nov 30 '21 15:11 graemerocher

@sbrannen do all JUnit 5 annotations need registering for reflection?

The annotation search algorithms in JUnit 5 do indeed use a lot of reflection in order to support meta-annotations.

Thus, any repeatable annotation (such as the @Property one from Micronaut) will need to be opened up for reflection if such annotations are present on methods, constructors, fields, etc. in test classes or test interfaces.

Even if such annotations are not actually used in the tests, JUnit will still use reflection to introspect them.

sbrannen avatar Dec 06 '21 15:12 sbrannen

Did you have to add reflection configuration for java.lang.annotation.Retention and java.lang.annotation.Target because of JUnit?

If so, that seems like a bug to me, and I'd like to fix that in JUnit if necessary.

sbrannen avatar Dec 06 '21 16:12 sbrannen

Even if such annotations are not actually used in the tests, JUnit will still use reflection to introspect them.

In light of that fact, we could probably make the UX better by automatically registering such annotations for reflection.

We already do quite a bit in the config package for the Feature.

sbrannen avatar Dec 06 '21 16:12 sbrannen

@graemerocher, is this still relevant on recent versions of GraalVM?

sbrannen avatar Sep 19 '22 14:09 sbrannen

I haven't tested without our workaround in place to be honest, will need to try

graemerocher avatar Sep 19 '22 15:09 graemerocher

@graemerocher, is this still relevant on recent versions of GraalVM?

Ping!

sbrannen avatar Aug 19 '23 10:08 sbrannen

can't reproduce anymore so closing. Thanks.

graemerocher avatar Aug 22 '23 09:08 graemerocher