Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report initialization failures per test method #1672

Merged
merged 1 commit into from Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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