org.mockito.runners.MockitoJUnitRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-core Show documentation
Show all versions of mockito-core Show documentation
Mock objects library for java
/*
* 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.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.runners.RunnerFactory;
import org.mockito.internal.runners.RunnerImpl;
import java.lang.reflect.InvocationTargetException;
/**
* Compatible with JUnit 4.4 and higher, this runner adds following behavior:
*
* -
* Initializes mocks annotated with {@link Mock},
* so that explicit usage of {@link MockitoAnnotations#initMocks(Object)} is not necessary.
* Mocks are initialized before each test method.
*
-
* validates framework usage after each test method. See javadoc for {@link Mockito#validateMockitoUsage()}.
*
*
* Runner is completely optional - there are other ways you can get @Mock working, for example by writing a base class.
* Explicitly validating framework usage is also optional because it is triggered automatically by Mockito every time you use the framework.
* See javadoc for {@link Mockito#validateMockitoUsage()}.
*
* Read more about @Mock annotation in javadoc for {@link MockitoAnnotations}
*
* @RunWith(MockitoJUnitRunner.class)
* public class ExampleTest {
*
* @Mock
* private List list;
*
* @Test
* public void shouldDoSomething() {
* list.add(100);
* }
* }
*
*/
public class MockitoJUnitRunner extends Runner implements Filterable {
private final RunnerImpl runner;
public MockitoJUnitRunner(Class> klass) throws InvocationTargetException {
runner = new RunnerFactory().create(klass);
}
@Override
public void run(final RunNotifier notifier) {
runner.run(notifier);
}
@Override
public Description getDescription() {
return runner.getDescription();
}
public void filter(Filter filter) throws NoTestsRemainException {
//filter is required because without it UnrootedTests show up in Eclipse
runner.filter(filter);
}
}