• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.quality;
6 
7 import org.mockito.Incubating;
8 import org.mockito.MockitoSession;
9 import org.mockito.exceptions.misusing.PotentialStubbingProblem;
10 import org.mockito.exceptions.misusing.UnnecessaryStubbingException;
11 import org.mockito.internal.junit.JUnitRule;
12 import org.mockito.junit.MockitoJUnit;
13 import org.mockito.junit.MockitoJUnitRunner;
14 import org.mockito.junit.MockitoRule;
15 
16 /**
17  * Configures the "strictness" of Mockito, affecting the behavior of stubbings and verification.
18  * "Strict stubbing" is a new feature in Mockito 2 that drives cleaner tests and better productivity.
19  * The easiest way to leverage it is via Mockito's JUnit support ({@link MockitoJUnit}) or Mockito Session ({@link MockitoSession}).
20  * <p>
21  * How strictness influences the behavior of the test?
22  * <ol>
23  *     <li>{@link Strictness#STRICT_STUBS} - ensures clean tests, reduces test code duplication, improves debuggability.
24  *       Best combination of flexibility and productivity. Highly recommended.
25  *       Planned as default for Mockito v3.
26  *       Enable it via {@link MockitoRule}, {@link MockitoJUnitRunner} or {@link MockitoSession}.
27  *       See {@link #STRICT_STUBS} for the details.</li>
28  *     <li>{@link Strictness#LENIENT} - no added behavior.
29  *       The default of Mockito 1.x.
30  *       Recommended only if you cannot use {@link #STRICT_STUBS}</li>
31  *     <li>{@link Strictness#WARN} - cleaner tests but only if you read the console output.
32  *       Reports console warnings about unused stubs
33  *       and stubbing argument mismatch (see {@link org.mockito.quality.MockitoHint}).
34  *       The default behavior of Mockito 2.x when {@link JUnitRule} or {@link MockitoJUnitRunner} are used.
35  *       Recommended if you cannot use {@link #STRICT_STUBS}.
36  *       Introduced originally with Mockito 2 because console warnings was the only compatible way of adding such feature.</li>
37  * </ol>
38  *
39  * @since 2.3.0
40  */
41 @Incubating
42 public enum Strictness {
43 
44     /**
45      * No extra strictness. Mockito 1.x behavior.
46      * Recommended only if you cannot use {@link #STRICT_STUBS}.
47      * <p>
48      * For more information see {@link Strictness}.
49      *
50      * @since 2.3.0
51      */
52     @Incubating
53     LENIENT,
54 
55     /**
56      * Helps keeping tests clean and improves debuggability only if you read the console output.
57      * Extra warnings emitted to the console, see {@link MockitoHint}.
58      * Default Mockito 2.x behavior.
59      * Recommended only if you cannot use {@link #STRICT_STUBS} because console output is ignored most of the time.
60      * <p>
61      * For more information see {@link Strictness}.
62      *
63      * @since 2.3.0
64      */
65     @Incubating
66     WARN,
67 
68     /**
69      * Ensures clean tests, reduces test code duplication, improves debuggability.
70      * Offers best combination of flexibility and productivity.
71      * Highly recommended.
72      * Planned as default for Mockito v3.
73      * Enable it via our JUnit support ({@link MockitoJUnit}) or {@link MockitoSession}.
74      * <p>
75      * Adds following behavior:
76      *  <ul>
77      *      <li>Improved productivity: the test fails early when code under test invokes
78      *          stubbed method with different arguments (see {@link PotentialStubbingProblem}).</li>
79      *      <li>Cleaner tests without unnecessary stubbings:
80      *          the test fails when unused stubs are present (see {@link UnnecessaryStubbingException}).</li>
81      *      <li>Cleaner, more DRY tests ("Don't Repeat Yourself"):
82      *          If you use {@link org.mockito.Mockito#verifyNoMoreInteractions(Object...)}
83      *          you no longer need to explicitly verify stubbed invocations.
84      *          They are automatically verified for you.</li>
85      *  </ul>
86      *
87      * For more information see {@link Strictness}.
88      *
89      * @since 2.3.0
90      */
91     @Incubating
92     STRICT_STUBS;
93 }
94