Skip to content

Commit

Permalink
Add test to show BlockJUnit4ClassRunner#createTest() is called by def…
Browse files Browse the repository at this point in the history
…ault by BlockJUnit4ClassRunner#createTest(FrameworkMethod)
  • Loading branch information
petergeneric committed Dec 2, 2014
1 parent 13ce5d1 commit dd0595b
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,44 @@ public void testMethodB() {
public void overrideCreateTestMethod() {
assertThat(testResult(OverrideCreateTest.class), isSuccessful());
}


/**
* Runner for testing override of {@link org.junit.runners.BlockJUnit4ClassRunner#createTest()}
* is still called by default if no other {@code createTest} method override
* is in place. This is tested by setting a boolean flag in a field of the
* test class so it can be checked to confirm that the createTest method was
* called.
*/
public static class CreateTestDefersToNoArgCreateTestRunner extends BlockJUnit4ClassRunner {
public CreateTestDefersToNoArgCreateTestRunner(final Class<?> klass) throws InitializationError {
super(klass);

assert(klass.equals(CreateTestDefersToNoArgCreateTestTest.class));
}

@Override
protected Object createTest() {
final CreateTestDefersToNoArgCreateTestTest obj = new CreateTestDefersToNoArgCreateTestTest();

obj.createTestCalled = true;

return obj;
}
}

@RunWith(CreateTestDefersToNoArgCreateTestRunner.class)
public static class CreateTestDefersToNoArgCreateTestTest {
public boolean createTestCalled = false;

@Test
public void testCreateTestCalled() {
assertEquals(true, createTestCalled);
}
}

@Test
public void createTestDefersToNoArgCreateTest() {
assertThat(testResult(CreateTestDefersToNoArgCreateTestTest.class), isSuccessful());
}
}

0 comments on commit dd0595b

Please sign in to comment.