Skip to content

Commit 52e813d

Browse files
committed
Improve structure and naming of ProxyExceptionHandlingTests
Closes gh-33797
1 parent f19a1b5 commit 52e813d

File tree

3 files changed

+100
-45
lines changed

3 files changed

+100
-45
lines changed

spring-aop/src/test/java/org/springframework/aop/framework/ProxyExceptionHandlingTests.java renamed to spring-aop/src/test/java/org/springframework/aop/framework/AbstractProxyExceptionHandlingTests.java

+23-45
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.aop.framework;
1818

19-
import java.lang.reflect.Proxy;
2019
import java.lang.reflect.UndeclaredThrowableException;
2120
import java.util.Objects;
2221

@@ -27,20 +26,22 @@
2726
import org.mockito.Mockito;
2827
import org.mockito.stubbing.Answer;
2928

30-
import org.springframework.cglib.proxy.Enhancer;
3129
import org.springframework.lang.Nullable;
3230

3331
import static org.assertj.core.api.Assertions.assertThat;
3432
import static org.assertj.core.api.Assertions.catchThrowable;
35-
import static org.mockito.BDDMockito.doAnswer;
36-
import static org.mockito.BDDMockito.doThrow;
37-
import static org.mockito.BDDMockito.mock;
33+
import static org.mockito.BDDMockito.willAnswer;
34+
import static org.mockito.BDDMockito.willThrow;
35+
import static org.mockito.Mockito.mock;
3836

3937
/**
4038
* @author Mikaël Francoeur
39+
* @author Sam Brannen
4140
* @since 6.2
41+
* @see JdkProxyExceptionHandlingTests
42+
* @see CglibProxyExceptionHandlingTests
4243
*/
43-
abstract class ProxyExceptionHandlingTests {
44+
abstract class AbstractProxyExceptionHandlingTests {
4445

4546
private static final RuntimeException uncheckedException = new RuntimeException();
4647

@@ -52,10 +53,8 @@ abstract class ProxyExceptionHandlingTests {
5253

5354
protected final ProxyFactory proxyFactory = new ProxyFactory(target);
5455

55-
@Nullable
5656
protected MyInterface proxy;
5757

58-
@Nullable
5958
private Throwable throwableSeenByCaller;
6059

6160

@@ -64,59 +63,38 @@ void clear() {
6463
Mockito.clearInvocations(target);
6564
}
6665

66+
6767
protected abstract void assertProxyType(Object proxy);
6868

69+
6970
private void invokeProxy() {
7071
throwableSeenByCaller = catchThrowable(() -> Objects.requireNonNull(proxy).doSomething());
7172
}
7273

7374
@SuppressWarnings("SameParameterValue")
74-
private Answer<?> sneakyThrow(Throwable throwable) {
75+
private static Answer<?> sneakyThrow(Throwable throwable) {
7576
return invocation -> {
7677
throw throwable;
7778
};
7879
}
7980

8081

81-
static class JdkAopProxyTests extends ProxyExceptionHandlingTests {
82-
83-
@Override
84-
protected void assertProxyType(Object proxy) {
85-
assertThat(Proxy.isProxyClass(proxy.getClass())).isTrue();
86-
}
87-
}
88-
89-
90-
static class CglibAopProxyTests extends ProxyExceptionHandlingTests {
91-
92-
@BeforeEach
93-
void setup() {
94-
proxyFactory.setProxyTargetClass(true);
95-
}
96-
97-
@Override
98-
protected void assertProxyType(Object proxy) {
99-
assertThat(Enhancer.isEnhanced(proxy.getClass())).isTrue();
100-
}
101-
}
102-
103-
10482
@Nested
105-
class WhenThereIsOneInterceptor {
83+
class WhenThereIsOneInterceptorTests {
10684

10785
@Nullable
10886
private Throwable throwableSeenByInterceptor;
10987

11088
@BeforeEach
11189
void beforeEach() {
11290
proxyFactory.addAdvice(captureThrowable());
113-
proxy = (MyInterface) proxyFactory.getProxy(ProxyExceptionHandlingTests.class.getClassLoader());
91+
proxy = (MyInterface) proxyFactory.getProxy(getClass().getClassLoader());
11492
assertProxyType(proxy);
11593
}
11694

11795
@Test
11896
void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
119-
doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething();
97+
willAnswer(sneakyThrow(undeclaredCheckedException)).given(target).doSomething();
12098
invokeProxy();
12199
assertThat(throwableSeenByInterceptor).isSameAs(undeclaredCheckedException);
122100
assertThat(throwableSeenByCaller)
@@ -126,15 +104,15 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
126104

127105
@Test
128106
void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException {
129-
doThrow(declaredCheckedException).when(target).doSomething();
107+
willThrow(declaredCheckedException).given(target).doSomething();
130108
invokeProxy();
131109
assertThat(throwableSeenByInterceptor).isSameAs(declaredCheckedException);
132110
assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException);
133111
}
134112

135113
@Test
136114
void targetThrowsUncheckedException() throws DeclaredCheckedException {
137-
doThrow(uncheckedException).when(target).doSomething();
115+
willThrow(uncheckedException).given(target).doSomething();
138116
invokeProxy();
139117
assertThat(throwableSeenByInterceptor).isSameAs(uncheckedException);
140118
assertThat(throwableSeenByCaller).isSameAs(uncheckedException);
@@ -155,17 +133,17 @@ private MethodInterceptor captureThrowable() {
155133

156134

157135
@Nested
158-
class WhenThereAreNoInterceptors {
136+
class WhenThereAreNoInterceptorsTests {
159137

160138
@BeforeEach
161139
void beforeEach() {
162-
proxy = (MyInterface) proxyFactory.getProxy(ProxyExceptionHandlingTests.class.getClassLoader());
140+
proxy = (MyInterface) proxyFactory.getProxy(getClass().getClassLoader());
163141
assertProxyType(proxy);
164142
}
165143

166144
@Test
167145
void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
168-
doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething();
146+
willAnswer(sneakyThrow(undeclaredCheckedException)).given(target).doSomething();
169147
invokeProxy();
170148
assertThat(throwableSeenByCaller)
171149
.isInstanceOf(UndeclaredThrowableException.class)
@@ -174,21 +152,21 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
174152

175153
@Test
176154
void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException {
177-
doThrow(declaredCheckedException).when(target).doSomething();
155+
willThrow(declaredCheckedException).given(target).doSomething();
178156
invokeProxy();
179157
assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException);
180158
}
181159

182160
@Test
183161
void targetThrowsUncheckedException() throws DeclaredCheckedException {
184-
doThrow(uncheckedException).when(target).doSomething();
162+
willThrow(uncheckedException).given(target).doSomething();
185163
invokeProxy();
186164
assertThat(throwableSeenByCaller).isSameAs(uncheckedException);
187165
}
188166
}
189167

190168

191-
protected interface MyInterface {
169+
interface MyInterface {
192170

193171
void doSomething() throws DeclaredCheckedException;
194172
}
@@ -202,11 +180,11 @@ public void doSomething() throws DeclaredCheckedException {
202180
}
203181

204182
@SuppressWarnings("serial")
205-
protected static class UndeclaredCheckedException extends Exception {
183+
private static class UndeclaredCheckedException extends Exception {
206184
}
207185

208186
@SuppressWarnings("serial")
209-
protected static class DeclaredCheckedException extends Exception {
187+
private static class DeclaredCheckedException extends Exception {
210188
}
211189

212190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.aop.framework;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
21+
import org.springframework.cglib.proxy.Enhancer;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* @author Mikaël Francoeur
27+
* @since 6.2
28+
* @see JdkProxyExceptionHandlingTests
29+
*/
30+
class CglibProxyExceptionHandlingTests extends AbstractProxyExceptionHandlingTests {
31+
32+
@BeforeEach
33+
void setup() {
34+
proxyFactory.setProxyTargetClass(true);
35+
}
36+
37+
@Override
38+
protected void assertProxyType(Object proxy) {
39+
assertThat(Enhancer.isEnhanced(proxy.getClass())).isTrue();
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.aop.framework;
18+
19+
import java.lang.reflect.Proxy;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* @author Mikaël Francoeur
25+
* @since 6.2
26+
* @see CglibProxyExceptionHandlingTests
27+
*/
28+
class JdkProxyExceptionHandlingTests extends AbstractProxyExceptionHandlingTests {
29+
30+
@Override
31+
protected void assertProxyType(Object proxy) {
32+
assertThat(Proxy.isProxyClass(proxy.getClass())).isTrue();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)