1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.runners;
6 
7 import org.junit.runner.Description;
8 import org.junit.runner.Runner;
9 import org.junit.runner.manipulation.Filter;
10 import org.junit.runner.manipulation.Filterable;
11 import org.junit.runner.manipulation.NoTestsRemainException;
12 import org.junit.runner.notification.Failure;
13 import org.junit.runner.notification.RunListener;
14 import org.junit.runner.notification.RunNotifier;
15 import org.mockito.internal.debugging.WarningsCollector;
16 import org.mockito.internal.runners.RunnerFactory;
17 import org.mockito.internal.runners.InternalRunner;
18 import org.mockito.internal.util.ConsoleMockitoLogger;
19 import org.mockito.internal.util.MockitoLogger;
20 
21 import java.lang.reflect.InvocationTargetException;
22 
23 /**
24  * @deprecated as of 2.1.0. Use the {@link org.mockito.junit.MockitoJUnitRunner} runner instead
25  * which contains support for detecting unused stubs.
26  * <p>
27  * If you still prefer using this runner, tell us why (create ticket in our issue tracker).
28  */
29 @Deprecated
30 public class ConsoleSpammingMockitoJUnitRunner extends Runner implements Filterable {
31 
32     private final MockitoLogger logger;
33     private final InternalRunner runner;
34 
ConsoleSpammingMockitoJUnitRunner(Class<?> klass)35     public ConsoleSpammingMockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
36         this(new ConsoleMockitoLogger(), new RunnerFactory().create(klass));
37     }
38 
ConsoleSpammingMockitoJUnitRunner(MockitoLogger logger, InternalRunner runner)39     ConsoleSpammingMockitoJUnitRunner(MockitoLogger logger, InternalRunner runner) {
40         this.runner = runner;
41         this.logger = logger;
42     }
43 
44     @Override
run(RunNotifier notifier)45     public void run(RunNotifier notifier) {
46         RunListener listener = new RunListener() {
47             WarningsCollector warningsCollector;
48 
49             @Override
50             public void testStarted(Description description) throws Exception {
51                 warningsCollector = new WarningsCollector();
52             }
53 
54             @Override public void testFailure(Failure failure) throws Exception {
55                 logger.log(warningsCollector.getWarnings());
56             }
57         };
58 
59         notifier.addListener(listener);
60 
61         runner.run(notifier);
62     }
63 
64     @Override
getDescription()65     public Description getDescription() {
66         return runner.getDescription();
67     }
68 
filter(Filter filter)69     public void filter(Filter filter) throws NoTestsRemainException {
70         //filter is required because without it UnrootedTests show up in Eclipse
71         runner.filter(filter);
72     }
73 }
74