Skip to content

Commit d652491

Browse files
committed
Fix stack overflow in SpringBootMockResolver
Closes gh-32632
1 parent 9d57cbc commit d652491

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/SpringBootMockResolver.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import org.mockito.plugins.MockResolver;
2020

21-
import org.springframework.test.util.AopTestUtils;
21+
import org.springframework.aop.TargetSource;
22+
import org.springframework.aop.framework.Advised;
23+
import org.springframework.aop.support.AopUtils;
24+
import org.springframework.util.Assert;
2225

2326
/**
24-
* A {@link MockResolver} for testing Spring Boot applications with Mockito. Resolves
25-
* mocks by returning the {@link AopTestUtils#getUltimateTargetObject(Object) ultimate
26-
* target object} of the instance.
27+
* A {@link MockResolver} for testing Spring Boot applications with Mockito. It resolves
28+
* mocks by walking the proxy chain until the target or a non-static proxy is found.
2729
*
2830
* @author Andy Wilkinson
2931
* @since 2.4.0
@@ -32,7 +34,28 @@ public class SpringBootMockResolver implements MockResolver {
3234

3335
@Override
3436
public Object resolve(Object instance) {
35-
return AopTestUtils.getUltimateTargetObject(instance);
37+
return getUltimateTargetObject(instance);
38+
}
39+
40+
@SuppressWarnings("unchecked")
41+
private static <T> T getUltimateTargetObject(Object candidate) {
42+
Assert.notNull(candidate, "Candidate must not be null");
43+
try {
44+
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised) {
45+
Advised advised = (Advised) candidate;
46+
TargetSource targetSource = advised.getTargetSource();
47+
if (targetSource.isStatic()) {
48+
Object target = targetSource.getTarget();
49+
if (target != null) {
50+
return getUltimateTargetObject(target);
51+
}
52+
}
53+
}
54+
}
55+
catch (Throwable ex) {
56+
throw new IllegalStateException("Failed to unwrap proxied object", ex);
57+
}
58+
return (T) candidate;
3659
}
3760

3861
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2012-2020 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.boot.test.mock.mockito;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aop.SpringProxy;
22+
import org.springframework.aop.framework.ProxyFactory;
23+
import org.springframework.aop.target.HotSwappableTargetSource;
24+
import org.springframework.aop.target.SingletonTargetSource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link SpringBootMockResolver}.
30+
*
31+
* @author Moritz Halbritter
32+
*/
33+
class SpringBootMockResolverTests {
34+
35+
@Test
36+
void testStaticTarget() {
37+
MyServiceImpl myService = new MyServiceImpl();
38+
MyService proxy = ProxyFactory.getProxy(MyService.class, new SingletonTargetSource(myService));
39+
Object target = new SpringBootMockResolver().resolve(proxy);
40+
assertThat(target).isInstanceOf(MyServiceImpl.class);
41+
}
42+
43+
@Test
44+
void testNonStaticTarget() {
45+
MyServiceImpl myService = new MyServiceImpl();
46+
MyService proxy = ProxyFactory.getProxy(MyService.class, new HotSwappableTargetSource(myService));
47+
Object target = new SpringBootMockResolver().resolve(proxy);
48+
assertThat(target).isInstanceOf(SpringProxy.class);
49+
}
50+
51+
private interface MyService {
52+
53+
int a();
54+
55+
}
56+
57+
private static class MyServiceImpl implements MyService {
58+
59+
@Override
60+
public int a() {
61+
return 1;
62+
}
63+
64+
}
65+
66+
}

0 commit comments

Comments
 (0)