Skip to content

Commit bd81abe

Browse files
committed
Merge branch '6.2.x'
2 parents fa01e9c + 51956fa commit bd81abe

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.context.bean.override.mockito.integration;
1818

1919
import org.junit.jupiter.api.Test;
20+
import org.mockito.Mockito;
2021

2122
import org.springframework.context.event.ContextRefreshedEvent;
2223
import org.springframework.context.event.EventListener;
@@ -25,6 +26,7 @@
2526
import org.springframework.test.context.bean.override.mockito.integration.MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests.ContextRefreshedEventListener;
2627
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2728

29+
import static org.assertj.core.api.Assertions.assertThat;
2830
import static org.mockito.ArgumentMatchers.any;
2931
import static org.mockito.BDDMockito.then;
3032

@@ -35,6 +37,7 @@
3537
* @author Sam Brannen
3638
* @author Yanming Zhou
3739
* @since 6.2.1
40+
* @see MockitoSpyBeanUsedDuringApplicationContextRefreshIntegrationTests
3841
*/
3942
@SpringJUnitConfig(ContextRefreshedEventListener.class)
4043
class MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests {
@@ -45,6 +48,8 @@ class MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests {
4548

4649
@Test
4750
void test() {
51+
assertThat(Mockito.mockingDetails(eventProcessor).isMock()).as("isMock").isTrue();
52+
assertThat(Mockito.mockingDetails(eventProcessor).isSpy()).as("isSpy").isFalse();
4853
// Ensure that the mock was invoked during ApplicationContext refresh
4954
// and has not been reset in the interim.
5055
then(eventProcessor).should().process(any(ContextRefreshedEvent.class));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.integration;
18+
19+
import org.junit.jupiter.api.AfterAll;
20+
import org.junit.jupiter.api.Test;
21+
import org.mockito.Mockito;
22+
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.Import;
26+
import org.springframework.context.event.ContextRefreshedEvent;
27+
import org.springframework.context.event.EventListener;
28+
import org.springframework.stereotype.Component;
29+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
30+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.mockito.ArgumentMatchers.same;
34+
import static org.mockito.BDDMockito.then;
35+
36+
/**
37+
* Integration tests for {@link MockitoSpyBean @MockitoSpyBean} used during
38+
* {@code ApplicationContext} refresh.
39+
*
40+
* @author Sam Brannen
41+
* @since 6.2.1
42+
* @see MockitoBeanUsedDuringApplicationContextRefreshIntegrationTests
43+
*/
44+
@SpringJUnitConfig
45+
class MockitoSpyBeanUsedDuringApplicationContextRefreshIntegrationTests {
46+
47+
static ContextRefreshedEvent contextRefreshedEvent;
48+
49+
@MockitoSpyBean
50+
ContextRefreshedEventProcessor eventProcessor;
51+
52+
53+
@AfterAll
54+
static void clearStaticField() {
55+
contextRefreshedEvent = null;
56+
}
57+
58+
@Test
59+
void test() {
60+
assertThat(Mockito.mockingDetails(eventProcessor).isSpy()).as("isSpy").isTrue();
61+
// Ensure that the spy was invoked during ApplicationContext refresh
62+
// and has not been reset in the interim.
63+
then(eventProcessor).should().process(same(contextRefreshedEvent));
64+
}
65+
66+
67+
@Configuration
68+
@Import(ContextRefreshedEventListener.class)
69+
static class Config {
70+
71+
@Bean
72+
ContextRefreshedEventProcessor eventProcessor() {
73+
// Cannot be a lambda expression, since Mockito cannot create a spy for a lambda.
74+
return new ContextRefreshedEventProcessor() {
75+
76+
@Override
77+
public void process(ContextRefreshedEvent event) {
78+
contextRefreshedEvent = event;
79+
}
80+
};
81+
}
82+
}
83+
84+
interface ContextRefreshedEventProcessor {
85+
void process(ContextRefreshedEvent event);
86+
}
87+
88+
// MUST be annotated with @Component, due to EventListenerMethodProcessor.isSpringContainerClass().
89+
@Component
90+
record ContextRefreshedEventListener(ContextRefreshedEventProcessor contextRefreshedEventProcessor) {
91+
92+
@EventListener
93+
void onApplicationEvent(ContextRefreshedEvent event) {
94+
this.contextRefreshedEventProcessor.process(event);
95+
}
96+
}
97+
98+
}

0 commit comments

Comments
 (0)