16
16
17
17
package org .springframework .test .context .bean .override .mockito ;
18
18
19
- import java .time .format .DateTimeFormatter ;
20
19
import java .util .List ;
21
20
21
+ import org .junit .jupiter .api .Nested ;
22
22
import org .junit .jupiter .api .Test ;
23
23
import org .junit .jupiter .params .ParameterizedTest ;
24
24
import org .junit .jupiter .params .provider .Arguments ;
41
41
import static org .mockito .ArgumentMatchers .anyInt ;
42
42
import static org .mockito .BDDMockito .when ;
43
43
import static org .mockito .Mockito .mock ;
44
+ import static org .mockito .quality .Strictness .LENIENT ;
44
45
import static org .mockito .quality .Strictness .STRICT_STUBS ;
45
46
46
47
/**
47
48
* Integration tests ensuring unnecessary stubbing is reported in various
48
49
* cases where a strict style is chosen or assumed.
49
50
*
50
51
* @author Simon Baslé
52
+ * @author Sam Brannen
51
53
* @since 6.2
52
54
*/
53
55
class MockitoBeanSettingsStrictIntegrationTests {
54
56
55
57
@ ParameterizedTest
56
58
@ FieldSource ("strictCases" )
57
- void unusedStubbingIsReported (Class <?> forCase ) {
59
+ void unusedStubbingIsReported (Class <?> testCase , int startedCount , int failureCount ) {
58
60
Events events = EngineTestKit .engine ("junit-jupiter" )
59
- .selectors (selectClass (forCase ))
61
+ .selectors (selectClass (testCase ))
60
62
.execute ()
61
63
.testEvents ()
62
- .assertStatistics (stats -> stats .started (1 ).failed (1 ));
64
+ .assertStatistics (stats -> stats .started (startedCount ).failed (failureCount ));
63
65
64
- events .assertThatEvents ().haveExactly (1 ,
66
+ events .assertThatEvents ().haveExactly (failureCount ,
65
67
event (test ("unnecessaryStub" ),
66
68
finishedWithFailure (
67
69
instanceOf (UnnecessaryStubbingException .class ),
68
70
message (msg -> msg .contains ("Unnecessary stubbings detected." )))));
69
71
}
70
72
71
73
static final List <Arguments > strictCases = List .of (
72
- argumentSet ("explicit strictness" , ExplicitStrictness .class ),
73
- argumentSet ("implicit strictness with @MockitoBean on field" , ImplicitStrictnessWithMockitoBean .class )
74
+ argumentSet ("explicit strictness" , ExplicitStrictness .class , 1 , 1 ),
75
+ argumentSet ("explicit strictness on enclosing class" , ExplicitStrictnessEnclosingTestCase .class , 1 , 1 ),
76
+ argumentSet ("implicit strictness with @MockitoBean on field" , ImplicitStrictnessWithMockitoBean .class , 1 , 1 ),
77
+ // 3, 1 --> The tests in LenientStubbingNestedTestCase and InheritedLenientStubbingNestedTestCase
78
+ // should not result in an UnnecessaryStubbingException.
79
+ argumentSet ("implicit strictness overridden and inherited in @Nested test classes" ,
80
+ ImplicitStrictnessWithMockitoBeanEnclosingTestCase .class , 3 , 1 )
74
81
);
75
82
76
83
abstract static class BaseCase {
@@ -94,8 +101,36 @@ static class ExplicitStrictness extends BaseCase {
94
101
static class ImplicitStrictnessWithMockitoBean extends BaseCase {
95
102
96
103
@ MockitoBean
97
- @ SuppressWarnings ("unused" )
98
- DateTimeFormatter ignoredMock ;
104
+ Runnable ignoredMock ;
105
+ }
106
+
107
+ @ SpringJUnitConfig (Config .class )
108
+ @ DirtiesContext
109
+ @ MockitoBeanSettings (STRICT_STUBS )
110
+ static class ExplicitStrictnessEnclosingTestCase {
111
+
112
+ @ Nested
113
+ class NestedTestCase extends BaseCase {
114
+ }
115
+ }
116
+
117
+ @ SpringJUnitConfig (Config .class )
118
+ @ DirtiesContext
119
+ static class ImplicitStrictnessWithMockitoBeanEnclosingTestCase extends BaseCase {
120
+
121
+ @ MockitoBean
122
+ Runnable ignoredMock ;
123
+
124
+ @ Nested
125
+ // Overrides implicit STRICT_STUBS
126
+ @ MockitoBeanSettings (LENIENT )
127
+ class LenientStubbingNestedTestCase extends BaseCase {
128
+
129
+ @ Nested
130
+ // Inherits LENIENT
131
+ class InheritedLenientStubbingNestedTestCase extends BaseCase {
132
+ }
133
+ }
99
134
}
100
135
101
136
@ Configuration (proxyBeanMethods = false )
0 commit comments