A JUnit test runner that loads the test class and optionally specified classes in a separate ClassLoader
. This means that static members will effectively be reset between each test class, as new versions of those classes are loaded.
<dependency>
<groupId>com.binarytweed</groupId>
<artifactId>quarantining-test-runner</artifactId>
<version>0.0.3</version>
</dependency>
Check the latest version on Maven Central.
import com.binarytweed.test.Quarantine;
import com.binarytweed.test.QuarantiningRunner;
@RunWith(QuarantiningRunner.class)
@Quarantine({"com.binarytweed", "com.example.ClassName"})
public class MyIsolatedTest {
...
- Annotate your test class with
@RunWith(QuarantiningRunner.class)
. - Use
@Quarantine("com.example")
to specify patterns which separately-loaded class names should start with. - Optionally specify
@DelegateRunningTo(SomeCustomRunner.class)
to haveQuarantiningRunner
use anotherRunner
implementation. By default it usesorg.junit.runners.JUnit4
(which currently extendsBlockJUnit4ClassRunner
).
The test class and runner class are loaded using QuarantiningUrlClassLoader
. When either of these two classes reference a class that hasn't yet been loaded, QuarantiningUrlClassLoader
will be used to load the requested class.
It will delegate loading classes that do not match the patterns provided by @Quarantined
to its parent ClassLoader
; patterns that do match are loaded by itself. Because each test run uses a a distinct instance of QuarantiningUrlClassLoader
, any classes it loads are distinct from any previous runs.