graalvm / native-build-tools

Native-image plugins for various build tools
https://graalvm.github.io/native-build-tools/
Other
371 stars 60 forks source link

The junit-native does not support org.junit.rules.ExpectedException. #575

Open tzezula opened 9 months ago

tzezula commented 9 months ago

Describe the bug The native junit test fails if the org.junit.rules.ExpectedException or org.hamcrest.core.Every is used by the test.

 MethodSource [className = 'org.me.MainTest', methodName = 'testExpectedException', methodParameterTypes = '']
    => java.lang.Error: Cannot determine correct type for matchesSafely() method.
       org.hamcrest.internal.ReflectiveTypeFinder.findExpectedType(ReflectiveTypeFinder.java:49)
       org.hamcrest.TypeSafeMatcher.<init>(TypeSafeMatcher.java:40)
       org.hamcrest.TypeSafeMatcher.<init>(TypeSafeMatcher.java:22)

To Reproduce Uncompress the attached maven project and run mvn -Pnative package.

Expected behavior The tests from the attached project should also pass in native mode. Currently, they are failing.

Logs See comments in the src/test/java/org/me/MainTest.java

System Info:

Additional context The issue arises from org.hamcrest.TypeSafeMatcher and org.hamcrest.TypeSafeDiagnosingMatcher utilizing ReflectiveTypeFinder to locate the matchesSafely method in their subclasses during image execution. This problem can be resolved by employing a reflection configuration file. Although the reflection.json file is included in the test project, it is commented out in the pom.xml. Since the reflective access targets JUnit types, it should ideally be handled by the JUnitPlatformFeature rather than by the application itself.

One approach is to eliminate type safety, similar to what is implemented in the internal GraalVM JUnit support. However, this method requires a substitution, which is not a public API.

Alternatively, we can register all used subclasses of TypeSafeMatcher and TypeSafeDiagnosingMatcher for reflective access to all declared methods. Implemented here.

tzezula commented 9 months ago

Test project

tzezula commented 9 months ago

Pull request addressing this issue: link to the pull request