All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.runners.MockitoJUnitRunner Maven / Gradle / Ivy

There is a newer version: 5.13.0
Show newest version
/*
 * 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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy