1616
1717package org .springframework .boot .test .mock .mockito ;
1818
19+ import java .util .List ;
1920import java .util .UUID ;
2021
22+ import org .junit .jupiter .api .BeforeAll ;
23+ import org .junit .jupiter .api .BeforeEach ;
2124import org .junit .jupiter .api .Disabled ;
2225import org .junit .jupiter .api .MethodOrderer ;
2326import org .junit .jupiter .api .Nested ;
2427import org .junit .jupiter .api .Order ;
2528import org .junit .jupiter .api .Test ;
29+ import org .junit .jupiter .api .TestInstance ;
30+ import org .junit .jupiter .api .TestInstance .Lifecycle ;
2631import org .junit .jupiter .api .TestMethodOrder ;
2732import org .junit .jupiter .api .extension .ExtendWith ;
2833import org .mockito .Mock ;
2934import org .mockito .MockedStatic ;
3035
36+ import org .springframework .boot .test .context .TestConfiguration ;
37+ import org .springframework .context .annotation .Bean ;
38+ import org .springframework .context .annotation .Import ;
3139import org .springframework .test .context .junit .jupiter .SpringExtension ;
3240
3341import static org .assertj .core .api .Assertions .assertThat ;
42+ import static org .mockito .BDDMockito .given ;
3443
3544/**
3645 * Integration tests for {@link MockitoTestExecutionListener}.
@@ -42,7 +51,7 @@ class MockitoTestExecutionListenerIntegrationTests {
4251
4352 @ Nested
4453 @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
45- class DisabledTests {
54+ class MockedStaticTests {
4655
4756 private static final UUID uuid = UUID .randomUUID ();
4857
@@ -60,12 +69,311 @@ void shouldReturnConstantValueDisabled() {
6069
6170 @ Test
6271 @ Order (2 )
63- void shouldReturnConstantValue () {
72+ void shouldNotFailBecauseOfMockedStaticNotBeingClosed () {
6473 this .mockedStatic .when (UUID ::randomUUID ).thenReturn (uuid );
6574 UUID result = UUID .randomUUID ();
6675 assertThat (result ).isEqualTo (uuid );
6776 }
6877
6978 }
7079
80+ @ Nested
81+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
82+ class ConfigureMockInBeforeEach {
83+
84+ @ Mock
85+ private List <String > mock ;
86+
87+ @ BeforeEach
88+ void setUp () {
89+ given (this .mock .size ()).willReturn (1 );
90+ }
91+
92+ @ Test
93+ @ Order (1 )
94+ void shouldUseSetUpConfiguration () {
95+ assertThat (this .mock .size ()).isEqualTo (1 );
96+ }
97+
98+ @ Test
99+ @ Order (2 )
100+ void shouldBeAbleToReconfigureMock () {
101+ given (this .mock .size ()).willReturn (2 );
102+ assertThat (this .mock .size ()).isEqualTo (2 );
103+ }
104+
105+ @ Test
106+ @ Order (3 )
107+ void shouldNotBeAffectedByOtherTests () {
108+ assertThat (this .mock .size ()).isEqualTo (1 );
109+ }
110+
111+ }
112+
113+ @ Nested
114+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
115+ @ TestInstance (Lifecycle .PER_CLASS )
116+ class ConfigureMockInBeforeAll {
117+
118+ @ Mock
119+ private List <String > mock ;
120+
121+ @ BeforeAll
122+ void setUp () {
123+ given (this .mock .size ()).willReturn (1 );
124+ }
125+
126+ @ Test
127+ @ Order (1 )
128+ void shouldUseSetUpConfiguration () {
129+ assertThat (this .mock .size ()).isEqualTo (1 );
130+ }
131+
132+ @ Test
133+ @ Order (2 )
134+ void shouldBeAbleToReconfigureMock () {
135+ given (this .mock .size ()).willReturn (2 );
136+ assertThat (this .mock .size ()).isEqualTo (2 );
137+ }
138+
139+ @ Test
140+ @ Order (3 )
141+ void shouldNotBeAffectedByOtherTest () {
142+ assertThat (this .mock .size ()).isEqualTo (2 );
143+ }
144+
145+ }
146+
147+ @ Nested
148+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
149+ @ Import (MyBeanConfiguration .class )
150+ class ConfigureMockBeanWithResetAfterInBeforeEach {
151+
152+ @ MockBean (reset = MockReset .AFTER )
153+ private MyBean mock ;
154+
155+ @ BeforeEach
156+ void setUp () {
157+ given (this .mock .call ()).willReturn (1 );
158+ }
159+
160+ @ Test
161+ @ Order (1 )
162+ void shouldUseSetUpConfiguration () {
163+ assertThat (this .mock .call ()).isEqualTo (1 );
164+ }
165+
166+ @ Test
167+ @ Order (2 )
168+ void shouldBeAbleToReconfigureMock () {
169+ given (this .mock .call ()).willReturn (2 );
170+ assertThat (this .mock .call ()).isEqualTo (2 );
171+ }
172+
173+ @ Test
174+ @ Order (3 )
175+ void shouldNotBeAffectedByOtherTests () {
176+ assertThat (this .mock .call ()).isEqualTo (1 );
177+ }
178+
179+ }
180+
181+ @ Nested
182+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
183+ @ Import (MyBeanConfiguration .class )
184+ class ConfigureMockBeanWithResetBeforeInBeforeEach {
185+
186+ @ MockBean (reset = MockReset .BEFORE )
187+ private MyBean mock ;
188+
189+ @ BeforeEach
190+ void setUp () {
191+ given (this .mock .call ()).willReturn (1 );
192+ }
193+
194+ @ Test
195+ @ Order (1 )
196+ void shouldUseSetUpConfiguration () {
197+ assertThat (this .mock .call ()).isEqualTo (1 );
198+ }
199+
200+ @ Test
201+ @ Order (2 )
202+ void shouldBeAbleToReconfigureMock () {
203+ given (this .mock .call ()).willReturn (2 );
204+ assertThat (this .mock .call ()).isEqualTo (2 );
205+ }
206+
207+ @ Test
208+ @ Order (3 )
209+ void shouldNotBeAffectedByOtherTests () {
210+ assertThat (this .mock .call ()).isEqualTo (1 );
211+ }
212+
213+ }
214+
215+ @ Nested
216+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
217+ @ Import (MyBeanConfiguration .class )
218+ class ConfigureMockBeanWithResetNoneInBeforeEach {
219+
220+ @ MockBean (reset = MockReset .NONE )
221+ private MyBean mock ;
222+
223+ @ BeforeEach
224+ void setUp () {
225+ given (this .mock .call ()).willReturn (1 );
226+ }
227+
228+ @ Test
229+ @ Order (1 )
230+ void shouldUseSetUpConfiguration () {
231+ assertThat (this .mock .call ()).isEqualTo (1 );
232+ }
233+
234+ @ Test
235+ @ Order (2 )
236+ void shouldBeAbleToReconfigureMock () {
237+ given (this .mock .call ()).willReturn (2 );
238+ assertThat (this .mock .call ()).isEqualTo (2 );
239+ }
240+
241+ @ Test
242+ @ Order (3 )
243+ void shouldNotBeAffectedByOtherTests () {
244+ assertThat (this .mock .call ()).isEqualTo (1 );
245+ }
246+
247+ }
248+
249+ @ Nested
250+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
251+ @ TestInstance (Lifecycle .PER_CLASS )
252+ @ Import (MyBeanConfiguration .class )
253+ class ConfigureMockBeanWithResetAfterInBeforeAll {
254+
255+ @ MockBean (reset = MockReset .AFTER )
256+ private MyBean mock ;
257+
258+ @ BeforeAll
259+ void setUp () {
260+ given (this .mock .call ()).willReturn (1 );
261+ }
262+
263+ @ Test
264+ @ Order (1 )
265+ void shouldUseSetUpConfiguration () {
266+ assertThat (this .mock .call ()).isEqualTo (1 );
267+ }
268+
269+ @ Test
270+ @ Order (2 )
271+ void shouldBeAbleToReconfigureMock () {
272+ given (this .mock .call ()).willReturn (2 );
273+ assertThat (this .mock .call ()).isEqualTo (2 );
274+ }
275+
276+ @ Test
277+ @ Order (3 )
278+ void shouldResetMockAfterReconfiguration () {
279+ assertThat (this .mock .call ()).isEqualTo (0 );
280+ }
281+
282+ }
283+
284+ @ Nested
285+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
286+ @ TestInstance (Lifecycle .PER_CLASS )
287+ @ Import (MyBeanConfiguration .class )
288+ class ConfigureMockBeanWithResetBeforeInBeforeAll {
289+
290+ @ MockBean (reset = MockReset .BEFORE )
291+ private MyBean mock ;
292+
293+ @ BeforeAll
294+ void setUp () {
295+ given (this .mock .call ()).willReturn (1 );
296+ }
297+
298+ @ Test
299+ @ Order (1 )
300+ void shouldResetMockBeforeThisMethod () {
301+ assertThat (this .mock .call ()).isEqualTo (0 );
302+ }
303+
304+ @ Test
305+ @ Order (2 )
306+ void shouldBeAbleToReconfigureMock () {
307+ given (this .mock .call ()).willReturn (2 );
308+ assertThat (this .mock .call ()).isEqualTo (2 );
309+ }
310+
311+ @ Test
312+ @ Order (3 )
313+ void shouldResetMockAfterReconfiguration () {
314+ assertThat (this .mock .call ()).isEqualTo (0 );
315+ }
316+
317+ }
318+
319+ @ Nested
320+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
321+ @ TestInstance (Lifecycle .PER_CLASS )
322+ @ Import (MyBeanConfiguration .class )
323+ class ConfigureMockBeanWithResetNoneInBeforeAll {
324+
325+ @ MockBean (reset = MockReset .NONE )
326+ private MyBean mock ;
327+
328+ @ BeforeAll
329+ void setUp () {
330+ given (this .mock .call ()).willReturn (1 );
331+ }
332+
333+ @ Test
334+ @ Order (1 )
335+ void shouldUseSetUpConfiguration () {
336+ assertThat (this .mock .call ()).isEqualTo (1 );
337+ }
338+
339+ @ Test
340+ @ Order (2 )
341+ void shouldBeAbleToReconfigureMock () {
342+ given (this .mock .call ()).willReturn (2 );
343+ assertThat (this .mock .call ()).isEqualTo (2 );
344+ }
345+
346+ @ Test
347+ @ Order (3 )
348+ void shouldNotResetMock () {
349+ assertThat (this .mock .call ()).isEqualTo (2 );
350+ }
351+
352+ }
353+
354+ interface MyBean {
355+
356+ int call ();
357+
358+ }
359+
360+ private static final class DefaultMyBean implements MyBean {
361+
362+ @ Override
363+ public int call () {
364+ return -1 ;
365+ }
366+
367+ }
368+
369+ @ TestConfiguration (proxyBeanMethods = false )
370+ private static final class MyBeanConfiguration {
371+
372+ @ Bean
373+ MyBean myBean () {
374+ return new DefaultMyBean ();
375+ }
376+
377+ }
378+
71379}
0 commit comments