Package org.mockito.quality
Enum Strictness
- java.lang.Object
-
- java.lang.Enum<Strictness>
-
- org.mockito.quality.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?
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 viaMockitoRule
,MockitoJUnitRunner
orMockitoSession
. SeeSTRICT_STUBS
for the details.LENIENT
- no added behavior. The default of Mockito 1.x. Recommended only if you cannot useSTRICT_STUBS
WARN
- cleaner tests but only if you read the console output. Reports console warnings about unused stubs and stubbing argument mismatch (seeMockitoHint
). The default behavior of Mockito 2.x whenJUnitRule
orMockitoJUnitRunner
are used. Recommended if you cannot useSTRICT_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.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Strictness
valueOf(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.
-
-
-
Enum Constant Detail
-
LENIENT
public static final Strictness LENIENT
No extra strictness. Mockito 1.x behavior. Recommended only if you cannot useSTRICT_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, seeMockitoHint
. Default Mockito 2.x behavior. Recommended only if you cannot useSTRICT_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. Enable it via our JUnit support (MockitoJUnit
) orMockitoSession
.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.
Strictness
.- Since:
- 2.3.0
- Improved productivity: the test fails early when code under test invokes
stubbed method with different arguments (see
-
-
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 nameNullPointerException
- if the argument is null
-
-