Class JUnitRule

  • All Implemented Interfaces:
    org.junit.rules.MethodRule, MockitoRule

    public final class JUnitRule
    extends java.lang.Object
    implements MockitoRule
    Internal implementation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base, org.junit.runners.model.FrameworkMethod method, java.lang.Object target)  
      MockitoRule silent()
      Rule will not report stubbing warnings during test execution.
      MockitoRule strictness​(Strictness strictness)
      The strictness, especially "strict stubs" (Strictness.STRICT_STUBS) helps debugging and keeping tests clean.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JUnitRule

        public JUnitRule​(MockitoLogger logger,
                         Strictness strictness)
        Parameters:
        strictness - how strict mocking / stubbing is concerned
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base,
                                                       org.junit.runners.model.FrameworkMethod method,
                                                       java.lang.Object target)
        Specified by:
        apply in interface org.junit.rules.MethodRule
      • silent

        public MockitoRule silent()
        Description copied from interface: MockitoRule
        Rule will not report stubbing warnings during test execution. By default, stubbing warnings are printed to Standard output to help debugging. Equivalent of configuring MockitoRule.strictness(Strictness) with Strictness.LENIENT.

        Please give us feedback about the stubbing warnings of JUnit rules by commenting on GitHub issue 769. It's a new feature of Mockito 2.1.0. It aims to help debugging tests. We want to make sure the feature is useful. We would really like to know why do you wish to silence the warnings! See also MockitoHint.

        Example:

        
         public class ExampleTest {
        
             @Rule
             public MockitoRule rule = MockitoJUnit.rule().silent();
        
         }
         
        Specified by:
        silent in interface MockitoRule
      • strictness

        public MockitoRule strictness​(Strictness strictness)
        Description copied from interface: MockitoRule
        The strictness, especially "strict stubs" (Strictness.STRICT_STUBS) helps debugging and keeping tests clean. It's a new feature introduced in Mockito 2.3. Other levels of strictness - "warn" - (Strictness.WARN) and "lenient" (MockitoRule.silent()) strictness were already present in Mockito 2.1.0. Version 2.3.0 introduces "strict stubs" (Strictness.STRICT_STUBS).
        
         public class ExampleTest {
             @Rule
             public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
         }
         
        See Javadoc for Strictness to learn how strictness influences the behavior of the JUnit rule. See Strictness.STRICT_STUBS to learn why is it recommended to use "strict stubbing".

        It is possible to tweak the strictness per test method. Why would you need it? See the use cases in Javadoc for PotentialStubbingProblem class. In order to tweak strictness per stubbing see Mockito.lenient(), per mock see MockSettings.lenient().

        
         public class ExampleTest {
             @Rule
             public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
        
             @Test public void exampleTest() {
                 //Change the strictness level only for this test method
                 //Useful for edge cases (see Javadoc for PotentialStubbingProblem class)
                 mockito.strictness(Strictness.LENIENT);
        
                 //remaining test code
             }
         }
         
        "Strict stubs" are planned to be the default for Mockito v3
      • We are very eager to hear feedback about "strict stubbing" feature, let us know by commenting on GitHub issue 769. Strict stubbing is an attempt to improve testability and productivity with Mockito. Tell us what you think!
Specified by:
strictness in interface MockitoRule