org.mockito.runners.ConsoleSpammingMockitoJUnitRunner Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.runners;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.mockito.internal.debugging.DebuggingInfo;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.internal.runners.RunnerFactory;
import org.mockito.internal.runners.RunnerImpl;
import org.mockito.internal.util.MockitoLogger;
import org.mockito.internal.util.MockitoLoggerImpl;
import java.lang.reflect.InvocationTargetException;
/**
* Uses JUnit 4.5 runner {@link BlockJUnit4ClassRunner}.
*
* Experimental implementation that suppose to improve tdd/testing experience.
* Don't hesitate to send feedback to [email protected]
* It is very likely it will change in the next version!
*
* This runner does exactly what {@link MockitoJUnitRunner} does but also
* prints warnings that might be useful.
* The point is that Mockito should help the tdd developer to quickly figure out if the test fails for the right reason.
* Then the developer can implement the functionality.
* Also when the test fails it should be easy to figure out why the test fails.
*
* Sometimes when the test fails, the underlying reason is that stubbed method was called with wrong arguments.
* Sometimes it fails because one forgets to stub a method or forgets to call a stubbed method.
* All above problems are not immediately obvious.
*
* One way of approaching this problem is full-blown 'expect' API.
* However it means the 'expectations upfront' business which is not in line with core Mockito concepts.
* After all, the essence of testing are explicit assertions that are described consistently at the bottom of the test method.
*
* Here's the experiment: a warning is printed to the standard output if the test fails.
* Also, you get a clickabe link to the line of code. You can immediately jump to the place in code where the potential problem is.
*
* Let's say your test fails on assertion.
* Let's say the underlying reason is a stubbed method that was called with different arguments:
*
* //test:
* Dictionary dictionary = new Dictionary(translator);
* when(translator.translate("Mockito")).thenReturn("cool framework");
* String translated = dictionary.search("Mockito");
* assertEquals("cool framework", translated);
*
* //code:
* public String search(String word) {
* ...
* return translator.translate("oups");
*
*
* On standard output you'll see something like that:
*
* [Mockito] Warning - stubbed method called with different arguments.
* Stubbed this way:
* translator.translate("Mockito");
* org.dictionary.SmartDictionaryTest.shouldFindTranslation(SmartDictionaryTest.java:27)
*
* But called with different arguments:
* translator.translate("oups");
* org.dictionary.SmartDictionary.search(SmartDictionary.java:15)
*
*
* Note that it is just a warning, not an assertion.
* The test fails on assertion because it's the assertion's duty to document what the test stands for and what behavior it proves.
* Warnings just makes it quicker to figure out if the test fails for the right reason.
*
* Note that code links printed to the console are clickable in any decent IDE (e.g. Eclipse).
*
* So far I identified 3 cases when warnings are printed:
*
unstubbed method
* unsued stub
* stubbed method but called with different arguments
*
*
*
* Do you think it is useful or not? Drop us an email at [email protected]
*/
public class ConsoleSpammingMockitoJUnitRunner extends Runner {
private final MockitoLogger logger;
private RunnerImpl runner;
public ConsoleSpammingMockitoJUnitRunner(Class klass) throws InvocationTargetException {
this(new MockitoLoggerImpl(), new RunnerFactory().create(klass));
}
ConsoleSpammingMockitoJUnitRunner(MockitoLogger logger, RunnerImpl runnerImpl) {
this.runner = runnerImpl;
this.logger = logger;
}
@Override
public void run(RunNotifier notifier) {
MockingProgress progress = new ThreadSafeMockingProgress();
DebuggingInfo debuggingInfo = progress.getDebuggingInfo();
beforeRun(notifier, debuggingInfo);
runner.run(notifier);
afterRun(debuggingInfo);
}
private void afterRun(final DebuggingInfo debuggingInfo) {
debuggingInfo.clearData();
}
private void beforeRun(RunNotifier notifier, final DebuggingInfo debuggingInfo) {
debuggingInfo.collectData();
RunListener listener = new RunListener() {
@Override public void testFailure(Failure failure) throws Exception {
debuggingInfo.printWarnings(logger);
}
};
notifier.addListener(listener);
}
@Override
public Description getDescription() {
return runner.getDescription();
}
}