forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Honor MockReset strategy for @MockitoBean and @MockitoSpyBean
Commit 6c2cba5 introduced a regression by inadvertently removing the MockReset strategy comparison when resetting @MockitoBean and @MockitoSpyBean mocks. This commit reinstates the MockReset strategy check and introduces tests for this feature. Closes spring-projectsgh-33941
- Loading branch information
Showing
7 changed files
with
220 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...ringframework/test/context/bean/override/mockito/MockResetStrategiesIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.bean.override.mockito; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
import org.junit.jupiter.api.extension.AfterEachCallback; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
|
||
import org.springframework.test.context.bean.override.mockito.MockResetStrategiesIntegrationTests.MockVerificationExtension; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
/** | ||
* Integration tests for {@link MockitoBean @MockitoBean} fields with different | ||
* {@link MockReset} strategies. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.2.1 | ||
* @see MockitoResetTestExecutionListenerWithoutMockitoAnnotationsIntegrationTests | ||
* @see MockitoResetTestExecutionListenerWithMockitoBeanIntegrationTests | ||
*/ | ||
// The MockVerificationExtension MUST be registered before the SpringExtension. | ||
@ExtendWith(MockVerificationExtension.class) | ||
@ExtendWith(SpringExtension.class) | ||
@TestMethodOrder(MethodOrderer.MethodName.class) | ||
class MockResetStrategiesIntegrationTests { | ||
|
||
static PuzzleService puzzleServiceNoneStaticReference; | ||
static PuzzleService puzzleServiceBeforeStaticReference; | ||
static PuzzleService puzzleServiceAfterStaticReference; | ||
|
||
|
||
@MockitoBean(name = "puzzleServiceNone", reset = MockReset.NONE) | ||
PuzzleService puzzleServiceNone; | ||
|
||
@MockitoBean(name = "puzzleServiceBefore", reset = MockReset.BEFORE) | ||
PuzzleService puzzleServiceBefore; | ||
|
||
@MockitoBean(name = "puzzleServiceAfter", reset = MockReset.AFTER) | ||
PuzzleService puzzleServiceAfter; | ||
|
||
|
||
@AfterEach | ||
void trackStaticReferences() { | ||
puzzleServiceNoneStaticReference = this.puzzleServiceNone; | ||
puzzleServiceBeforeStaticReference = this.puzzleServiceBefore; | ||
puzzleServiceAfterStaticReference = this.puzzleServiceAfter; | ||
} | ||
|
||
@AfterAll | ||
static void releaseStaticReferences() { | ||
puzzleServiceNoneStaticReference = null; | ||
puzzleServiceBeforeStaticReference = null; | ||
puzzleServiceAfterStaticReference = null; | ||
} | ||
|
||
|
||
@Test | ||
void test001(TestInfo testInfo) { | ||
assertThat(puzzleServiceNone.getAnswer()).isNull(); | ||
assertThat(puzzleServiceBefore.getAnswer()).isNull(); | ||
assertThat(puzzleServiceAfter.getAnswer()).isNull(); | ||
|
||
stubAndTestMocks(testInfo); | ||
} | ||
|
||
@Test | ||
void test002(TestInfo testInfo) { | ||
// Should not have been reset. | ||
assertThat(puzzleServiceNone.getAnswer()).isEqualTo("none - test001"); | ||
|
||
// Should have been reset. | ||
assertThat(puzzleServiceBefore.getAnswer()).isNull(); | ||
assertThat(puzzleServiceAfter.getAnswer()).isNull(); | ||
|
||
stubAndTestMocks(testInfo); | ||
} | ||
|
||
private void stubAndTestMocks(TestInfo testInfo) { | ||
String name = testInfo.getTestMethod().get().getName(); | ||
given(puzzleServiceNone.getAnswer()).willReturn("none - " + name); | ||
assertThat(puzzleServiceNone.getAnswer()).isEqualTo("none - " + name); | ||
|
||
given(puzzleServiceBefore.getAnswer()).willReturn("before - " + name); | ||
assertThat(puzzleServiceBefore.getAnswer()).isEqualTo("before - " + name); | ||
|
||
given(puzzleServiceAfter.getAnswer()).willReturn("after - " + name); | ||
assertThat(puzzleServiceAfter.getAnswer()).isEqualTo("after - " + name); | ||
} | ||
|
||
interface PuzzleService { | ||
|
||
String getAnswer(); | ||
} | ||
|
||
static class MockVerificationExtension implements AfterEachCallback { | ||
|
||
@Override | ||
public void afterEach(ExtensionContext context) throws Exception { | ||
String name = context.getRequiredTestMethod().getName(); | ||
|
||
// Should not have been reset. | ||
assertThat(puzzleServiceNoneStaticReference.getAnswer()).as("puzzleServiceNone").isEqualTo("none - " + name); | ||
assertThat(puzzleServiceBeforeStaticReference.getAnswer()).as("puzzleServiceBefore").isEqualTo("before - " + name); | ||
|
||
// Should have been reset. | ||
assertThat(puzzleServiceAfterStaticReference.getAnswer()).as("puzzleServiceAfter").isNull(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...e/mockito/integration/MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2002-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.bean.override.mockito.integration; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.test.context.bean.override.mockito.MockitoBean; | ||
import org.springframework.test.context.bean.override.mockito.integration.MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.ContextRefreshedEventListener; | ||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.then; | ||
|
||
/** | ||
* Integration tests for {@link MockitoBean @MockitoBean} used during | ||
* {@code ApplicationContext} refresh. | ||
* | ||
* @author Sam Brannen | ||
* @author Yanming Zhou | ||
* @since 6.2.1 | ||
*/ | ||
@SpringJUnitConfig(ContextRefreshedEventListener.class) | ||
class MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests { | ||
|
||
@MockitoBean | ||
ContextRefreshedEventProcessor eventProcessor; | ||
|
||
|
||
@Test | ||
void test() { | ||
// Ensure that the mock was invoked during ApplicationContext refresh | ||
// and has not been reset in the interim. | ||
then(eventProcessor).should().process(any(ContextRefreshedEvent.class)); | ||
} | ||
|
||
|
||
interface ContextRefreshedEventProcessor { | ||
void process(ContextRefreshedEvent event); | ||
} | ||
|
||
// MUST be annotated with @Component, due to EventListenerMethodProcessor.isSpringContainerClass(). | ||
@Component | ||
record ContextRefreshedEventListener(ContextRefreshedEventProcessor contextRefreshedEventProcessor) { | ||
|
||
@EventListener | ||
void onApplicationEvent(ContextRefreshedEvent event) { | ||
this.contextRefreshedEventProcessor.process(event); | ||
} | ||
} | ||
|
||
} |