1616
1717package org .springframework .aop .framework ;
1818
19- import java .lang .reflect .Proxy ;
2019import java .lang .reflect .UndeclaredThrowableException ;
2120import java .util .Objects ;
2221
2726import org .mockito .Mockito ;
2827import org .mockito .stubbing .Answer ;
2928
30- import org .springframework .cglib .proxy .Enhancer ;
3129import org .springframework .lang .Nullable ;
3230
3331import static org .assertj .core .api .Assertions .assertThat ;
3432import 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}
0 commit comments