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

org.mockito.junit.MockitoJUnitRule Maven / Gradle / Ivy

There is a newer version: 2.0.2-beta
Show newest version
package org.mockito.junit;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.mockito.internal.rules.JUnitRule;

import static org.mockito.internal.util.Checks.checkNotNull;

/**
 * The MockitoJUnitRule Rule can be used instead of {@link org.mockito.runners.MockitoJUnitRunner}.
 * Requires JUnit 4.9 (at least).
 *
 * This rule adds following behavior:
 * 
    *
  • * Initializes mocks annotated with {@link org.mockito.Mock}, * so that explicit usage of {@link org.mockito.MockitoAnnotations#initMocks(Object)} is not necessary. * Mocks are initialized before each test method. *
  • * validates framework usage after each test method. See javadoc for {@link org.mockito.Mockito#validateMockitoUsage()}. *
* Example use: *

 * public class ExampleTest {
 *
 *     @Rule
 *     public MockitoJUnitRule mockitoJUnitRule = new MockitoJUnitRule(this);
 *
 *     @Mock
 *     private List list;
 *
 *     @Test
 *     public void shouldDoSomething() {
 *         list.add(100);
 *     }
 * }
 * 
*/ public class MockitoJUnitRule implements TestRule { private final JUnitRule jUnitRule; /** * @param targetTest the test class instance where the rule is declared. Cannot be null. */ public MockitoJUnitRule(Object targetTest) { checkNotNull(targetTest, "Mockito JUnit rule target"); this.jUnitRule = new JUnitRule(targetTest); } public Statement apply(final Statement base, Description description) { return jUnitRule.apply(base, description); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy