Description
The default tests runner (if not specified) is BlockJUnit4ClassRunner its default behavior is to create a new instance of the test class for each executed test method. This can be changed by deriving an ExtendedBlockJUnit4ClassRunner and overriding its createTest method:
public class ExtendedBlockJUnit4ClassRunner { private final AtomicReference<Pair<Class<?>, Object>> testHolder = new AtomicReference<>(); protected Object createTest() throws Exception { Pair<Class<?>, Object> lastTest = testHolder.get(); Class<?> lastTestClass = (lastTest == null) ? null : lastTest.getLeft(); TestClass curTest = getTestClass(): Class<?> curTestClass = curTest.getJavaClass(): if (curTestClass == lastTestClass) { return lastTest.getRight(); } Object instance = curTest.getOnlyConstructor().newInstance(); testHolder.set(Pair.of(curTestClass, instance)); return instance; }