ClassNotFoundException when using JUnit 5 ParametrizedTest with MethodSource on Java 11 with bnd

Has anybody already tried running JUnit 5 Parametrized tests that use a @MethodSource in Java 11 with bnd?

My tests ran fine with Java 8.

However, switching to Java 11, I got a ClassNotFoundException for the class under test.

Debugging showed that JUnit 5 was trying to look up the class using the thread context class loader (TCCL) which was set to the OSGI ContextFinder and the only CL that one would consider was the CL of the JUnit 5 platform bundle.

If I manually set the TCCL using a @BeforeAll method to the classloader of the class under test, it works.

I was curious if anybody has already run into this or a similar problem.

Problem occurred for me when running the tests using the BND Eclipse Plugins as well as when building on the command line using the BND Maven Plugins.

@kriegfrj do you have any idea?

Hi @rec, thanks for the post (and thanks @pkriens for bringing it to my attention!)

Without seeing the actual test case that is causing the issue I cannot be 100% certain, but based on what you’ve said I’m 90% certain that this is a manifestation of these issues in JUnit:

These issues should have been resolved in PR: Revised PR for #3279 and #3292 - custom class loader arrangements by sbrannen · Pull Request #3297 · junit-team/junit5 · GitHub

I think that it will solve your problem. I don’t think this commit has made it into a public release of JUnit yet, but hopefully in the next. In the meantime, the TCCL trick that you did in @BeforeAll should work (as long as you’re un-setting it in @AfterAll), as long as you’re not trying to run JUnit multithreaded. The fix that was committed in PR #3297 should be multi-threaded safe, I think.

Hope that helps!

1 Like

@kriegfrj Thanks for the explanation and pointers. I’ll try remembering to check this again when the next JUnit version has been released.