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

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

Open tzezula opened this issue 1 year ago • 2 comments

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:

  • OS: macos
  • GraalVM Version 23.1.2
  • Java Version 21
  • Plugin version native-maven-plugin:0.10.0

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 avatar Feb 19 '24 17:02 tzezula