1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.runners;
6
7import org.junit.runner.Description;
8import org.junit.runner.Runner;
9import org.junit.runner.manipulation.Filter;
10import org.junit.runner.manipulation.Filterable;
11import org.junit.runner.manipulation.NoTestsRemainException;
12import org.junit.runner.notification.RunNotifier;
13import org.mockito.Mock;
14import org.mockito.Mockito;
15import org.mockito.MockitoAnnotations;
16import org.mockito.internal.runners.RunnerFactory;
17import org.mockito.internal.runners.RunnerImpl;
18
19import java.lang.reflect.InvocationTargetException;
20
21
22/**
23 * Compatible with <b>JUnit 4.4 and higher</b>, this runner adds following behavior:
24 * <ul>
25 *   <li>
26 *      Initializes mocks annotated with {@link Mock},
27 *      so that explicit usage of {@link MockitoAnnotations#initMocks(Object)} is not necessary.
28 *      Mocks are initialized before each test method.
29 *   <li>
30 *      validates framework usage after each test method. See javadoc for {@link Mockito#validateMockitoUsage()}.
31 * </ul>
32 *
33 * Runner is completely optional - there are other ways you can get &#064;Mock working, for example by writing a base class.
34 * Explicitly validating framework usage is also optional because it is triggered automatically by Mockito every time you use the framework.
35 * See javadoc for {@link Mockito#validateMockitoUsage()}.
36 * <p>
37 * Read more about &#064;Mock annotation in javadoc for {@link MockitoAnnotations}
38 * <pre class="code"><code class="java">
39 * <b>&#064;RunWith(MockitoJUnitRunner.class)</b>
40 * public class ExampleTest {
41 *
42 *     &#064;Mock
43 *     private List list;
44 *
45 *     &#064;Test
46 *     public void shouldDoSomething() {
47 *         list.add(100);
48 *     }
49 * }
50 * </code></pre>
51 */
52public class MockitoJUnitRunner extends Runner implements Filterable {
53
54    private final RunnerImpl runner;
55
56    public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
57        runner = new RunnerFactory().create(klass);
58    }
59
60    @Override
61    public void run(final RunNotifier notifier) {
62        runner.run(notifier);
63    }
64
65    @Override
66    public Description getDescription() {
67        return runner.getDescription();
68    }
69
70	public void filter(Filter filter) throws NoTestsRemainException {
71        //filter is required because without it UnrootedTests show up in Eclipse
72		runner.filter(filter);
73	}
74}