|
| 1 | +/* |
| 2 | + * Copyright 2002-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.test.context.bean.override.mockito; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.AfterAll; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.MethodOrderer; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.TestInfo; |
| 24 | +import org.junit.jupiter.api.TestMethodOrder; |
| 25 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 28 | + |
| 29 | +import org.springframework.test.context.bean.override.mockito.MockResetStrategiesIntegrationTests.MockVerificationExtension; |
| 30 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.mockito.BDDMockito.given; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration tests for {@link MockitoBean @MockitoBean} fields with different |
| 37 | + * {@link MockReset} strategies. |
| 38 | + * |
| 39 | + * @author Sam Brannen |
| 40 | + * @since 6.2.1 |
| 41 | + * @see MockitoResetTestExecutionListenerWithoutMockitoAnnotationsIntegrationTests |
| 42 | + * @see MockitoResetTestExecutionListenerWithMockitoBeanIntegrationTests |
| 43 | + */ |
| 44 | +// The MockVerificationExtension MUST be registered before the SpringExtension. |
| 45 | +@ExtendWith(MockVerificationExtension.class) |
| 46 | +@ExtendWith(SpringExtension.class) |
| 47 | +@TestMethodOrder(MethodOrderer.MethodName.class) |
| 48 | +class MockResetStrategiesIntegrationTests { |
| 49 | + |
| 50 | + static PuzzleService puzzleServiceNoneStaticReference; |
| 51 | + static PuzzleService puzzleServiceBeforeStaticReference; |
| 52 | + static PuzzleService puzzleServiceAfterStaticReference; |
| 53 | + |
| 54 | + |
| 55 | + @MockitoBean(name = "puzzleServiceNone", reset = MockReset.NONE) |
| 56 | + PuzzleService puzzleServiceNone; |
| 57 | + |
| 58 | + @MockitoBean(name = "puzzleServiceBefore", reset = MockReset.BEFORE) |
| 59 | + PuzzleService puzzleServiceBefore; |
| 60 | + |
| 61 | + @MockitoBean(name = "puzzleServiceAfter", reset = MockReset.AFTER) |
| 62 | + PuzzleService puzzleServiceAfter; |
| 63 | + |
| 64 | + |
| 65 | + @AfterEach |
| 66 | + void trackStaticReferences() { |
| 67 | + puzzleServiceNoneStaticReference = this.puzzleServiceNone; |
| 68 | + puzzleServiceBeforeStaticReference = this.puzzleServiceBefore; |
| 69 | + puzzleServiceAfterStaticReference = this.puzzleServiceAfter; |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + static void releaseStaticReferences() { |
| 74 | + puzzleServiceNoneStaticReference = null; |
| 75 | + puzzleServiceBeforeStaticReference = null; |
| 76 | + puzzleServiceAfterStaticReference = null; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Test |
| 81 | + void test001(TestInfo testInfo) { |
| 82 | + assertThat(puzzleServiceNone.getAnswer()).isNull(); |
| 83 | + assertThat(puzzleServiceBefore.getAnswer()).isNull(); |
| 84 | + assertThat(puzzleServiceAfter.getAnswer()).isNull(); |
| 85 | + |
| 86 | + stubAndTestMocks(testInfo); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void test002(TestInfo testInfo) { |
| 91 | + // Should not have been reset. |
| 92 | + assertThat(puzzleServiceNone.getAnswer()).isEqualTo("none - test001"); |
| 93 | + |
| 94 | + // Should have been reset. |
| 95 | + assertThat(puzzleServiceBefore.getAnswer()).isNull(); |
| 96 | + assertThat(puzzleServiceAfter.getAnswer()).isNull(); |
| 97 | + |
| 98 | + stubAndTestMocks(testInfo); |
| 99 | + } |
| 100 | + |
| 101 | + private void stubAndTestMocks(TestInfo testInfo) { |
| 102 | + String name = testInfo.getTestMethod().get().getName(); |
| 103 | + given(puzzleServiceNone.getAnswer()).willReturn("none - " + name); |
| 104 | + assertThat(puzzleServiceNone.getAnswer()).isEqualTo("none - " + name); |
| 105 | + |
| 106 | + given(puzzleServiceBefore.getAnswer()).willReturn("before - " + name); |
| 107 | + assertThat(puzzleServiceBefore.getAnswer()).isEqualTo("before - " + name); |
| 108 | + |
| 109 | + given(puzzleServiceAfter.getAnswer()).willReturn("after - " + name); |
| 110 | + assertThat(puzzleServiceAfter.getAnswer()).isEqualTo("after - " + name); |
| 111 | + } |
| 112 | + |
| 113 | + interface PuzzleService { |
| 114 | + |
| 115 | + String getAnswer(); |
| 116 | + } |
| 117 | + |
| 118 | + static class MockVerificationExtension implements AfterEachCallback { |
| 119 | + |
| 120 | + @Override |
| 121 | + public void afterEach(ExtensionContext context) throws Exception { |
| 122 | + String name = context.getRequiredTestMethod().getName(); |
| 123 | + |
| 124 | + // Should not have been reset. |
| 125 | + assertThat(puzzleServiceNoneStaticReference.getAnswer()).as("puzzleServiceNone").isEqualTo("none - " + name); |
| 126 | + assertThat(puzzleServiceBeforeStaticReference.getAnswer()).as("puzzleServiceBefore").isEqualTo("before - " + name); |
| 127 | + |
| 128 | + // Should have been reset. |
| 129 | + assertThat(puzzleServiceAfterStaticReference.getAnswer()).as("puzzleServiceAfter").isNull(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments