Skip to content

Commit d1d6ff8

Browse files
committed
Verify @⁠MockitoSpyBean can be used with circular dependencies
See gh-33742
1 parent 9f0dbc4 commit d1d6ff8

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.context.annotation.Import;
24+
import org.springframework.test.context.aot.DisabledInAotMode;
25+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
26+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
27+
28+
import static org.mockito.BDDMockito.then;
29+
30+
/**
31+
* Tests that {@link MockitoSpyBean @MockitoSpyBean} can be used to replace an
32+
* existing bean with circular dependencies with {@link Autowired @Autowired}
33+
* setter methods.
34+
*
35+
* @author Andy Wilkinson
36+
* @author Sam Brannen
37+
* @since 6.2
38+
* @see MockitoSpyBeanAndCircularDependenciesWithLazyResolutionProxyIntegrationTests
39+
*/
40+
@SpringJUnitConfig
41+
@DisabledInAotMode // Circular dependencies cannot be resolved in AOT mode unless a @Lazy resolution proxy is used.
42+
class MockitoSpyBeanAndCircularDependenciesWithAutowiredSettersIntegrationTests {
43+
44+
@MockitoSpyBean
45+
One one;
46+
47+
@Autowired
48+
Two two;
49+
50+
51+
@Test
52+
void beanWithCircularDependenciesCanBeSpied() {
53+
two.callOne();
54+
then(one).should().doSomething();
55+
}
56+
57+
58+
@Configuration
59+
@Import({ One.class, Two.class })
60+
static class Config {
61+
}
62+
63+
static class One {
64+
65+
@SuppressWarnings("unused")
66+
private Two two;
67+
68+
@Autowired
69+
void setTwo(Two two) {
70+
this.two = two;
71+
}
72+
73+
void doSomething() {
74+
}
75+
}
76+
77+
static class Two {
78+
79+
private One one;
80+
81+
@Autowired
82+
void setOne(One one) {
83+
this.one = one;
84+
}
85+
86+
void callOne() {
87+
this.one.doSomething();
88+
}
89+
}
90+
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.context.annotation.Import;
24+
import org.springframework.context.annotation.Lazy;
25+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
26+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
27+
28+
import static org.mockito.BDDMockito.then;
29+
30+
/**
31+
* Tests that {@link MockitoSpyBean @MockitoSpyBean} can be used to replace an
32+
* existing bean with circular dependencies with {@link Autowired @Autowired}
33+
* setter methods and a {@link Lazy @Lazy} resolution proxy.
34+
*
35+
* <p>In contrast to {@link MockitoSpyBeanAndCircularDependenciesWithAutowiredSettersIntegrationTests},
36+
* this test class works in AOT mode.
37+
*
38+
* @author Sam Brannen
39+
* @author Andy Wilkinson
40+
* @since 6.2
41+
* @see MockitoSpyBeanAndCircularDependenciesWithAutowiredSettersIntegrationTests
42+
*/
43+
@SpringJUnitConfig
44+
class MockitoSpyBeanAndCircularDependenciesWithLazyResolutionProxyIntegrationTests {
45+
46+
@MockitoSpyBean
47+
One one;
48+
49+
@Autowired
50+
Two two;
51+
52+
53+
@Test
54+
void beanWithCircularDependenciesCanBeSpied() {
55+
two.callOne();
56+
then(one).should().doSomething();
57+
}
58+
59+
60+
@Configuration
61+
@Import({ One.class, Two.class })
62+
static class Config {
63+
}
64+
65+
static class One {
66+
67+
@SuppressWarnings("unused")
68+
private Two two;
69+
70+
@Autowired
71+
void setTwo(@Lazy Two two) {
72+
this.two = two;
73+
}
74+
75+
void doSomething() {
76+
}
77+
}
78+
79+
static class Two {
80+
81+
private One one;
82+
83+
@Autowired
84+
void setOne(One one) {
85+
this.one = one;
86+
}
87+
88+
void callOne() {
89+
this.one.doSomething();
90+
}
91+
}
92+
93+
}

0 commit comments

Comments
 (0)