mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Ensure that remaining mocks are closed before initializing a test
Closes gh-39271
This commit is contained in:
parent
930669888a
commit
df755db1f3
@ -64,9 +64,10 @@ public class MockitoTestExecutionListener extends AbstractTestExecutionListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeTestMethod(TestContext testContext) {
|
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||||
if (Boolean.TRUE.equals(
|
if (Boolean.TRUE.equals(
|
||||||
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
|
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
|
||||||
|
closeMocks(testContext);
|
||||||
initMocks(testContext);
|
initMocks(testContext);
|
||||||
reinjectFields(testContext);
|
reinjectFields(testContext);
|
||||||
}
|
}
|
||||||
@ -77,6 +78,11 @@ public class MockitoTestExecutionListener extends AbstractTestExecutionListener
|
|||||||
closeMocks(testContext);
|
closeMocks(testContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterTestClass(TestContext testContext) throws Exception {
|
||||||
|
closeMocks(testContext);
|
||||||
|
}
|
||||||
|
|
||||||
private void initMocks(TestContext testContext) {
|
private void initMocks(TestContext testContext) {
|
||||||
if (hasMockitoAnnotations(testContext)) {
|
if (hasMockitoAnnotations(testContext)) {
|
||||||
testContext.setAttribute(MOCKS_ATTRIBUTE_NAME, MockitoAnnotations.openMocks(testContext.getTestInstance()));
|
testContext.setAttribute(MOCKS_ATTRIBUTE_NAME, MockitoAnnotations.openMocks(testContext.getTestInstance()));
|
||||||
|
@ -21,11 +21,13 @@ import java.util.UUID;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.ClassOrderer;
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.MethodOrderer;
|
import org.junit.jupiter.api.MethodOrderer;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Order;
|
import org.junit.jupiter.api.Order;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestClassOrder;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
import org.junit.jupiter.api.TestMethodOrder;
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
@ -36,6 +38,8 @@ import org.mockito.MockedStatic;
|
|||||||
import org.springframework.boot.test.context.TestConfiguration;
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
|
import org.springframework.test.annotation.DirtiesContext.ClassMode;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
@ -77,6 +81,122 @@ class MockitoTestExecutionListenerIntegrationTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
|
||||||
|
class MockedStaticTestsDirtiesContext {
|
||||||
|
|
||||||
|
private static final UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MockedStatic<UUID> mockedStatic;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
@Disabled
|
||||||
|
void shouldReturnConstantValueDisabled() {
|
||||||
|
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||||
|
UUID result = UUID.randomUUID();
|
||||||
|
assertThat(result).isEqualTo(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(2)
|
||||||
|
void shouldNotFailBecauseOfMockedStaticNotBeingClosed() {
|
||||||
|
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||||
|
UUID result = UUID.randomUUID();
|
||||||
|
assertThat(result).isEqualTo(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(3)
|
||||||
|
void shouldNotFailBecauseOfMockedStaticNotBeingClosedWhenMocksAreReinjected() {
|
||||||
|
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||||
|
UUID result = UUID.randomUUID();
|
||||||
|
assertThat(result).isEqualTo(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
|
||||||
|
class MockedStaticTestsIfClassContainsOnlyDisabledTests {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@Order(1)
|
||||||
|
class TestClass1 {
|
||||||
|
|
||||||
|
private static final UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MockedStatic<UUID> mockedStatic;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
@Disabled
|
||||||
|
void disabledTest() {
|
||||||
|
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@Order(2)
|
||||||
|
class TestClass2 {
|
||||||
|
|
||||||
|
private static final UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MockedStatic<UUID> mockedStatic;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
void shouldNotFailBecauseMockedStaticHasNotBeenClosed() {
|
||||||
|
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||||
|
UUID result = UUID.randomUUID();
|
||||||
|
assertThat(result).isEqualTo(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
|
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
|
||||||
|
class MockedStaticTestsIfClassContainsNoTests {
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@Order(1)
|
||||||
|
class TestClass1 {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MockedStatic<UUID> mockedStatic;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
@Order(2)
|
||||||
|
class TestClass2 {
|
||||||
|
|
||||||
|
private static final UUID uuid = UUID.randomUUID();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private MockedStatic<UUID> mockedStatic;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
void shouldNotFailBecauseMockedStaticHasNotBeenClosed() {
|
||||||
|
this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
|
||||||
|
UUID result = UUID.randomUUID();
|
||||||
|
assertThat(result).isEqualTo(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
class ConfigureMockInBeforeEach {
|
class ConfigureMockInBeforeEach {
|
||||||
|
Loading…
Reference in New Issue
Block a user