1616
1717package org .springframework .test .context .bean .override .mockito ;
1818
19- import java .time .format .DateTimeFormatter ;
2019import java .util .List ;
2120
21+ import org .junit .jupiter .api .Nested ;
2222import org .junit .jupiter .api .Test ;
2323import org .junit .jupiter .params .ParameterizedTest ;
2424import org .junit .jupiter .params .provider .Arguments ;
4141import static org .mockito .ArgumentMatchers .anyInt ;
4242import static org .mockito .BDDMockito .when ;
4343import static org .mockito .Mockito .mock ;
44+ import static org .mockito .quality .Strictness .LENIENT ;
4445import static org .mockito .quality .Strictness .STRICT_STUBS ;
4546
4647/**
4748 * Integration tests ensuring unnecessary stubbing is reported in various
4849 * cases where a strict style is chosen or assumed.
4950 *
5051 * @author Simon Baslé
52+ * @author Sam Brannen
5153 * @since 6.2
5254 */
5355class MockitoBeanSettingsStrictIntegrationTests {
5456
5557 @ ParameterizedTest
5658 @ FieldSource ("strictCases" )
57- void unusedStubbingIsReported (Class <?> forCase ) {
59+ void unusedStubbingIsReported (Class <?> testCase , int startedCount , int failureCount ) {
5860 Events events = EngineTestKit .engine ("junit-jupiter" )
59- .selectors (selectClass (forCase ))
61+ .selectors (selectClass (testCase ))
6062 .execute ()
6163 .testEvents ()
62- .assertStatistics (stats -> stats .started (1 ).failed (1 ));
64+ .assertStatistics (stats -> stats .started (startedCount ).failed (failureCount ));
6365
64- events .assertThatEvents ().haveExactly (1 ,
66+ events .assertThatEvents ().haveExactly (failureCount ,
6567 event (test ("unnecessaryStub" ),
6668 finishedWithFailure (
6769 instanceOf (UnnecessaryStubbingException .class ),
6870 message (msg -> msg .contains ("Unnecessary stubbings detected." )))));
6971 }
7072
7173 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 )
7481 );
7582
7683 abstract static class BaseCase {
@@ -94,8 +101,36 @@ static class ExplicitStrictness extends BaseCase {
94101 static class ImplicitStrictnessWithMockitoBean extends BaseCase {
95102
96103 @ 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+ }
99134 }
100135
101136 @ Configuration (proxyBeanMethods = false )
0 commit comments