Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
tzezula opened this issue Feb 19, 2024 · 2 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@tzezula
Copy link

tzezula commented Feb 19, 2024

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 tzezula added the bug Something isn't working label Feb 19, 2024
@tzezula
Copy link
Author

tzezula commented Feb 19, 2024

Test project

@tzezula
Copy link
Author

tzezula commented Feb 19, 2024

Pull request addressing this issue: link to the pull request

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant