Skip to content

Commit

Permalink
Merge pull request #1672 from marcphilipp/junit-4.13-compat
Browse files Browse the repository at this point in the history
Report initialization failures per test method
  • Loading branch information
TimvdLippe committed Mar 23, 2019
2 parents 7c8aae0 + 49e6311 commit d84bcb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
Expand Up @@ -30,34 +30,29 @@ public DefaultInternalRunner(Class<?> testClass, final Supplier<MockitoTestListe
public Object target;
private MockitoTestListener mockitoTestListener;

protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
protected Statement withBefores(FrameworkMethod method, final Object target, Statement statement) {
this.target = target;
// get new test listener and add it to the framework
mockitoTestListener = listenerSupplier.get();
Mockito.framework().addListener(mockitoTestListener);
// init annotated mocks before tests
MockitoAnnotations.initMocks(target);
return super.withBefores(method, target, statement);
final Statement base = super.withBefores(method, target, statement);
return new Statement() {
@Override
public void evaluate() throws Throwable {
// get new test listener and add it to the framework
mockitoTestListener = listenerSupplier.get();
Mockito.framework().addListener(mockitoTestListener);
// init annotated mocks before tests
MockitoAnnotations.initMocks(target);
base.evaluate();
}
};
}

public void run(final RunNotifier notifier) {
RunListener listener = new RunListener() {
private boolean started;
Throwable failure;

@Override
public void testStarted(Description description) throws Exception {
started = true;
}

@Override
public void testFailure(Failure failure) throws Exception {
this.failure = failure.getException();
// If the test fails during the setup, `testFinished` is never invoked
// Therefore, if we have not started, cleanup the testlistener
if (!started && mockitoTestListener != null) {
Mockito.framework().removeListener(mockitoTestListener);
}
}

@Override
Expand Down
Expand Up @@ -45,10 +45,10 @@ public void does_not_fail_second_test_when_first_test_fail() throws Exception {
.run(newNotifier(runListener));

verify(runListener, times(1)).testFailure(any(Failure.class));
verify(runListener, never()).testFinished(any(Description.class));
verify(mockitoTestListener, never()).testFinished(any(TestFinishedEvent.class));
verify(runListener, times(1)).testFinished(any(Description.class));
verify(mockitoTestListener, only()).testFinished(any(TestFinishedEvent.class));

reset(runListener);
reset(runListener, mockitoTestListener);

new DefaultInternalRunner(SuccessTest.class, supplier)
.run(newNotifier(runListener));
Expand Down

0 comments on commit d84bcb5

Please sign in to comment.