Skip to content

Commit c87d5ee

Browse files
committed
Ensure that remaining mocks are closed before initializing a test
Closes gh-39271
1 parent e1986ea commit c87d5ee

File tree

2 files changed

+83
-6
lines changed

2 files changed

+83
-6
lines changed

Diff for: spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListener.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*
4444
* @author Phillip Webb
4545
* @author Andy Wilkinson
46+
* @author Moritz Halbritter
4647
* @since 1.4.2
4748
* @see ResetMocksTestExecutionListener
4849
*/
@@ -57,12 +58,13 @@ public final int getOrder() {
5758

5859
@Override
5960
public void prepareTestInstance(TestContext testContext) throws Exception {
61+
closeMocks(testContext);
6062
initMocks(testContext);
6163
injectFields(testContext);
6264
}
6365

6466
@Override
65-
public void beforeTestMethod(TestContext testContext) throws Exception {
67+
public void beforeTestMethod(TestContext testContext) {
6668
if (Boolean.TRUE.equals(
6769
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
6870
initMocks(testContext);
@@ -72,10 +74,7 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
7274

7375
@Override
7476
public void afterTestMethod(TestContext testContext) throws Exception {
75-
Object mocks = testContext.getAttribute(MOCKS_ATTRIBUTE_NAME);
76-
if (mocks instanceof AutoCloseable closeable) {
77-
closeable.close();
78-
}
77+
closeMocks(testContext);
7978
}
8079

8180
private void initMocks(TestContext testContext) {
@@ -84,6 +83,13 @@ private void initMocks(TestContext testContext) {
8483
}
8584
}
8685

86+
private void closeMocks(TestContext testContext) throws Exception {
87+
Object mocks = testContext.getAttribute(MOCKS_ATTRIBUTE_NAME);
88+
if (mocks instanceof AutoCloseable closeable) {
89+
closeable.close();
90+
}
91+
}
92+
8793
private boolean hasMockitoAnnotations(TestContext testContext) {
8894
MockitoAnnotationCollection collector = new MockitoAnnotationCollection();
8995
ReflectionUtils.doWithFields(testContext.getTestClass(), collector);
@@ -126,7 +132,7 @@ private static final class MockitoAnnotationCollection implements FieldCallback
126132
private final Set<Annotation> annotations = new LinkedHashSet<>();
127133

128134
@Override
129-
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
135+
public void doWith(Field field) throws IllegalArgumentException {
130136
for (Annotation annotation : field.getDeclaredAnnotations()) {
131137
if (annotation.annotationType().getName().startsWith("org.mockito")) {
132138
this.annotations.add(annotation);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.mock.mockito;
18+
19+
import java.util.UUID;
20+
21+
import org.junit.jupiter.api.Disabled;
22+
import org.junit.jupiter.api.MethodOrderer;
23+
import org.junit.jupiter.api.Nested;
24+
import org.junit.jupiter.api.Order;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.TestMethodOrder;
27+
import org.junit.jupiter.api.extension.ExtendWith;
28+
import org.mockito.Mock;
29+
import org.mockito.MockedStatic;
30+
31+
import org.springframework.test.context.junit.jupiter.SpringExtension;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Integration tests for {@link MockitoTestExecutionListener}.
37+
*
38+
* @author Moritz Halbritter
39+
*/
40+
@ExtendWith(SpringExtension.class)
41+
class MockitoTestExecutionListenerIntegrationTests {
42+
43+
@Nested
44+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
45+
class DisabledTests {
46+
47+
private static final UUID uuid = UUID.randomUUID();
48+
49+
@Mock
50+
private MockedStatic<UUID> mockedStatic;
51+
52+
@Test
53+
@Order(1)
54+
@Disabled
55+
void shouldReturnConstantValueDisabled() {
56+
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
57+
UUID result = UUID.randomUUID();
58+
assertThat(result).isEqualTo(uuid);
59+
}
60+
61+
@Test
62+
@Order(2)
63+
void shouldReturnConstantValue() {
64+
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
65+
UUID result = UUID.randomUUID();
66+
assertThat(result).isEqualTo(uuid);
67+
}
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)