Enum Strictness

  • All Implemented Interfaces:
    Serializable, Comparable<Strictness>

    public enum Strictness
    extends 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 Detail

      • LENIENT

        public static final Strictness LENIENT
        No extra strictness. Mockito 1.x behavior. Recommended only if you cannot use STRICT_STUBS.

        For more information see Strictness.

        Since:
        2.3.0
      • WARN

        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

        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
    • 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​(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:
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null