Enum Strictness

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Strictness>

    @Incubating
    public enum Strictness
    extends java.lang.Enum<Strictness>
    Configures the "strictness" of Mockito, affecting the behavior of stubbings and verification. "Strict stubbing" is a new feature in Mockito 2 that drives cleaner tests and better productivity. The easiest way to leverage it is via Mockito's JUnit support (MockitoJUnit) or Mockito Session (MockitoSession).

    How strictness influences the behavior of the test?

    1. STRICT_STUBS - ensures clean tests, reduces test code duplication, improves debuggability. Best combination of flexibility and productivity. Highly recommended. Planned as default for Mockito v4. Enable it via MockitoRule, MockitoJUnitRunner or MockitoSession. See STRICT_STUBS for the details.
    2. LENIENT - no added behavior. The default of Mockito 1.x. Recommended only if you cannot use STRICT_STUBS
    3. WARN - cleaner tests but only if you read the console output. Reports console warnings about unused stubs and stubbing argument mismatch (see MockitoHint). The default behavior of Mockito 2.x when JUnitRule or MockitoJUnitRunner are used. Recommended if you cannot use STRICT_STUBS. Introduced originally with Mockito 2 because console warnings was the only compatible way of adding such feature.
    Since:
    2.3.0
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      LENIENT
      No extra strictness.
      STRICT_STUBS
      Ensures clean tests, reduces test code duplication, improves debuggability.
      WARN
      Helps keeping tests clean and improves debuggability only if you read the console output.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Strictness()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Strictness valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static Strictness[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • WARN

        @Incubating
        public static final Strictness WARN
        Helps keeping tests clean and improves debuggability only if you read the console output. Extra warnings emitted to the console, see MockitoHint. Default Mockito 2.x behavior. Recommended only if you cannot use STRICT_STUBS because console output is ignored most of the time.

        For more information see Strictness.

        Since:
        2.3.0
      • STRICT_STUBS

        @Incubating
        public static final Strictness STRICT_STUBS
        Ensures clean tests, reduces test code duplication, improves debuggability. Offers best combination of flexibility and productivity. Highly recommended. Planned as default for Mockito v4. Enable it via our JUnit support (MockitoJUnit) or MockitoSession.

        Adds following behavior:

        • Improved productivity: the test fails early when code under test invokes stubbed method with different arguments (see PotentialStubbingProblem).
        • Cleaner tests without unnecessary stubbings: the test fails when unused stubs are present (see UnnecessaryStubbingException).
        • Cleaner, more DRY tests ("Don't Repeat Yourself"): If you use Mockito.verifyNoMoreInteractions(Object...) you no longer need to explicitly verify stubbed invocations. They are automatically verified for you.
        For more information see Strictness.
        Since:
        2.3.0
    • Constructor Detail

      • Strictness

        private Strictness()
    • Method Detail

      • values

        public static Strictness[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (Strictness c : Strictness.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static Strictness valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null