Building upon John B's answer using TestContextManager
, one can also call beforeTestMethod()
and afterTestMethod()
on it to better simulate SpringJUnit4ClassRunner
's behaviour (for instance loading database with @Sql
).
These methods require a Method
parameter, so one can for instance take advantage of JUnit4's TestName
rule to get the current test method's name and then retrieving it by reflection.
private static TestContextManager springTestContext
= new TestContextManager(BlahTest.class);
@Rule
public TestName testName = new TestName();
@Before
public void before() throws Exception {
springTestContext.prepareTestInstance(this);
springTestContext.beforeTestMethod(this,
getClass().getMethod(testName.getMethodName()));
}
@After
public void after() throws Exception {
springTestContext.afterTestMethod(this,
getClass().getMethod(testName.getMethodName()), null);
}