1 package org.mockito.quality; 2 3 import org.mockito.Incubating; 4 import org.mockito.MockitoSession; 5 import org.mockito.exceptions.misusing.PotentialStubbingProblem; 6 import org.mockito.exceptions.misusing.UnnecessaryStubbingException; 7 import org.mockito.internal.junit.JUnitRule; 8 import org.mockito.junit.MockitoJUnitRunner; 9 import org.mockito.junit.MockitoRule; 10 11 /** 12 * Configures the "strictness" of Mockito during a mocking session. 13 * A session typically maps to a single test method invocation. 14 * {@code Strictness} drives cleaner tests and better productivity. 15 * The easiest way to leverage enhanced {@code Strictness} is using 16 * Mockito's JUnit support ({@link MockitoRule} or {@link MockitoJUnitRunner}). 17 * If you cannot use JUnit support {@link MockitoSession} is the way to go. 18 * <p> 19 * How strictness level influences the behavior of the test (mocking session)? 20 * <ol> 21 * <li>{@link Strictness#LENIENT} - no added behavior. 22 * The default of Mockito 1.x. 23 * Recommended only if you cannot use {@link #STRICT_STUBS} nor {@link #WARN}.</li> 24 * <li>{@link Strictness#WARN} - helps keeping tests clean and improves debuggability. 25 * Reports console warnings about unused stubs 26 * and stubbing argument mismatch (see {@link org.mockito.quality.MockitoHint}). 27 * The default behavior of Mockito 2.x when {@link JUnitRule} or {@link MockitoJUnitRunner} are used.</li> 28 * Recommended if you cannot use {@link #STRICT_STUBS}. 29 * <li>{@link Strictness#STRICT_STUBS} - ensures clean tests, reduces test code duplication, improves debuggability. 30 * Best combination of flexibility and productivity. Highly recommended. 31 * Planned as default for Mockito v3. 32 * See {@link #STRICT_STUBS} for the details. 33 * </ol> 34 * 35 * @since 2.3.0 36 */ 37 @Incubating 38 public enum Strictness { 39 40 /** 41 * No extra strictness. Mockito 1.x behavior. 42 * Recommended only if you cannot use {@link #STRICT_STUBS} nor {@link #WARN}. 43 * <p> 44 * For more information see {@link Strictness}. 45 * 46 * @since 2.3.0 47 */ 48 @Incubating 49 LENIENT, 50 51 /** 52 * Helps keeping tests clean and improves debuggability. 53 * Extra warnings emitted to the console, see {@link MockitoHint}. 54 * Default Mockito 2.x behavior. 55 * Recommended if you cannot use {@link #STRICT_STUBS}. 56 * <p> 57 * For more information see {@link Strictness}. 58 * 59 * @since 2.3.0 60 */ 61 @Incubating 62 WARN, 63 64 /** 65 * Ensures clean tests, reduces test code duplication, improves debuggability. 66 * Offers best combination of flexibility and productivity. 67 * Highly recommended. 68 * Planned as default for Mockito v3. 69 * <p> 70 * Adds following behavior: 71 * <ul> 72 * <li>Improved productivity: the test fails early when code under test invokes 73 * stubbed method with different arguments (see {@link PotentialStubbingProblem}).</li> 74 * <li>Cleaner tests without unnecessary stubbings: 75 * the test fails when unused stubs are present (see {@link UnnecessaryStubbingException}).</li> 76 * <li>Cleaner, more DRY tests ("Don't Repeat Yourself"): 77 * If you use {@link org.mockito.Mockito#verifyNoMoreInteractions(Object...)} 78 * you no longer need to explicitly verify stubbed invocations. 79 * They are automatically verified for you.</li> 80 * </ul> 81 * 82 * For more information see {@link Strictness}. 83 * 84 * @since 2.3.0 85 */ 86 @Incubating 87 STRICT_STUBS; 88 } 89