user14980174
user14980174

Reputation: 1

Sandboxed tests with Junit5 (Is Junit4 @RunWith more powerful compared to Junit5 @ExtendWith)

So with Junit4 I was able to run tests with my own runner via @RunWith annotation. This was super powerful and allowed me to use my own "special" class loader for each test. This "special" class loader job was to reload a subset of classes when those were accessed (actually it could implement different reloading policies but that's beside the point). What this effectively allowed me to achieve is running each test in a "sandbox".

I had multiple tests that were setting/requiring different values of static class variables running in parallel without stepping on each other toes.

My question is whether it's possible to achieve the same state of nirvana :) with a new Junit5?

Thank you very much for taking time to look and answer this.

My brute force attempt to use @ExtendWith along with custom implementation of TestInstanceFactory that was reloading class and returning instance of "reloaded" class failed miserably with the following exception:

org.junit.jupiter.api.extension.TestInstantiationException message: TestInstanceFactory [<my implementation of TestInstanceFactory class name>] failed to return an instance of [<my-test-class>@<hash as loaded by original loader>] and instead returned an instance of [<my-test-class>@<hash as loaded by my special loader>].

Upvotes: 0

Views: 204

Answers (1)

user14980174
user14980174

Reputation: 1

Apparently it's possible in Junit5. See github discussion with proposed solution: https://github.com/junit-team/junit5/issues/3028#issuecomment-1286880925

Upvotes: 0

Related Questions