|
21 | 21 |
|
22 | 22 | import org.junit.jupiter.api.BeforeAll;
|
23 | 23 | import org.junit.jupiter.api.BeforeEach;
|
| 24 | +import org.junit.jupiter.api.ClassOrderer; |
24 | 25 | import org.junit.jupiter.api.Disabled;
|
25 | 26 | import org.junit.jupiter.api.MethodOrderer;
|
26 | 27 | import org.junit.jupiter.api.Nested;
|
27 | 28 | import org.junit.jupiter.api.Order;
|
28 | 29 | import org.junit.jupiter.api.Test;
|
| 30 | +import org.junit.jupiter.api.TestClassOrder; |
29 | 31 | import org.junit.jupiter.api.TestInstance;
|
30 | 32 | import org.junit.jupiter.api.TestInstance.Lifecycle;
|
31 | 33 | import org.junit.jupiter.api.TestMethodOrder;
|
|
36 | 38 | import org.springframework.boot.test.context.TestConfiguration;
|
37 | 39 | import org.springframework.context.annotation.Bean;
|
38 | 40 | import org.springframework.context.annotation.Import;
|
| 41 | +import org.springframework.test.annotation.DirtiesContext; |
| 42 | +import org.springframework.test.annotation.DirtiesContext.ClassMode; |
39 | 43 | import org.springframework.test.context.junit.jupiter.SpringExtension;
|
40 | 44 |
|
41 | 45 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -77,6 +81,122 @@ void shouldNotFailBecauseOfMockedStaticNotBeingClosed() {
|
77 | 81 |
|
78 | 82 | }
|
79 | 83 |
|
| 84 | + @Nested |
| 85 | + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 86 | + @DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD) |
| 87 | + class MockedStaticTestsDirtiesContext { |
| 88 | + |
| 89 | + private static final UUID uuid = UUID.randomUUID(); |
| 90 | + |
| 91 | + @Mock |
| 92 | + private MockedStatic<UUID> mockedStatic; |
| 93 | + |
| 94 | + @Test |
| 95 | + @Order(1) |
| 96 | + @Disabled |
| 97 | + void shouldReturnConstantValueDisabled() { |
| 98 | + this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid); |
| 99 | + UUID result = UUID.randomUUID(); |
| 100 | + assertThat(result).isEqualTo(uuid); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @Order(2) |
| 105 | + void shouldNotFailBecauseOfMockedStaticNotBeingClosed() { |
| 106 | + this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid); |
| 107 | + UUID result = UUID.randomUUID(); |
| 108 | + assertThat(result).isEqualTo(uuid); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + @Order(3) |
| 113 | + void shouldNotFailBecauseOfMockedStaticNotBeingClosedWhenMocksAreReinjected() { |
| 114 | + this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid); |
| 115 | + UUID result = UUID.randomUUID(); |
| 116 | + assertThat(result).isEqualTo(uuid); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Nested |
| 122 | + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 123 | + @TestClassOrder(ClassOrderer.OrderAnnotation.class) |
| 124 | + class MockedStaticTestsIfClassContainsOnlyDisabledTests { |
| 125 | + |
| 126 | + @Nested |
| 127 | + @Order(1) |
| 128 | + class TestClass1 { |
| 129 | + |
| 130 | + private static final UUID uuid = UUID.randomUUID(); |
| 131 | + |
| 132 | + @Mock |
| 133 | + private MockedStatic<UUID> mockedStatic; |
| 134 | + |
| 135 | + @Test |
| 136 | + @Order(1) |
| 137 | + @Disabled |
| 138 | + void disabledTest() { |
| 139 | + this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid); |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + @Nested |
| 145 | + @Order(2) |
| 146 | + class TestClass2 { |
| 147 | + |
| 148 | + private static final UUID uuid = UUID.randomUUID(); |
| 149 | + |
| 150 | + @Mock |
| 151 | + private MockedStatic<UUID> mockedStatic; |
| 152 | + |
| 153 | + @Test |
| 154 | + @Order(1) |
| 155 | + void shouldNotFailBecauseMockedStaticHasNotBeenClosed() { |
| 156 | + this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid); |
| 157 | + UUID result = UUID.randomUUID(); |
| 158 | + assertThat(result).isEqualTo(uuid); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + } |
| 164 | + |
| 165 | + @Nested |
| 166 | + @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 167 | + @TestClassOrder(ClassOrderer.OrderAnnotation.class) |
| 168 | + class MockedStaticTestsIfClassContainsNoTests { |
| 169 | + |
| 170 | + @Nested |
| 171 | + @Order(1) |
| 172 | + class TestClass1 { |
| 173 | + |
| 174 | + @Mock |
| 175 | + private MockedStatic<UUID> mockedStatic; |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + @Nested |
| 180 | + @Order(2) |
| 181 | + class TestClass2 { |
| 182 | + |
| 183 | + private static final UUID uuid = UUID.randomUUID(); |
| 184 | + |
| 185 | + @Mock |
| 186 | + private MockedStatic<UUID> mockedStatic; |
| 187 | + |
| 188 | + @Test |
| 189 | + @Order(1) |
| 190 | + void shouldNotFailBecauseMockedStaticHasNotBeenClosed() { |
| 191 | + this.mockedStatic.when(UUID::randomUUID).thenReturn(uuid); |
| 192 | + UUID result = UUID.randomUUID(); |
| 193 | + assertThat(result).isEqualTo(uuid); |
| 194 | + } |
| 195 | + |
| 196 | + } |
| 197 | + |
| 198 | + } |
| 199 | + |
80 | 200 | @Nested
|
81 | 201 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
82 | 202 | class ConfigureMockInBeforeEach {
|
|
0 commit comments