16
16
17
17
package org .springframework .aop .framework ;
18
18
19
- import java .lang .reflect .Proxy ;
20
19
import java .lang .reflect .UndeclaredThrowableException ;
21
20
import java .util .Objects ;
22
21
27
26
import org .mockito .Mockito ;
28
27
import org .mockito .stubbing .Answer ;
29
28
30
- import org .springframework .cglib .proxy .Enhancer ;
31
29
import org .springframework .lang .Nullable ;
32
30
33
31
import static org .assertj .core .api .Assertions .assertThat ;
34
32
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 ;
38
36
39
37
/**
40
38
* @author Mikaël Francoeur
39
+ * @author Sam Brannen
41
40
* @since 6.2
41
+ * @see JdkProxyExceptionHandlingTests
42
+ * @see CglibProxyExceptionHandlingTests
42
43
*/
43
- abstract class ProxyExceptionHandlingTests {
44
+ abstract class AbstractProxyExceptionHandlingTests {
44
45
45
46
private static final RuntimeException uncheckedException = new RuntimeException ();
46
47
@@ -52,10 +53,8 @@ abstract class ProxyExceptionHandlingTests {
52
53
53
54
protected final ProxyFactory proxyFactory = new ProxyFactory (target );
54
55
55
- @ Nullable
56
56
protected MyInterface proxy ;
57
57
58
- @ Nullable
59
58
private Throwable throwableSeenByCaller ;
60
59
61
60
@@ -64,59 +63,38 @@ void clear() {
64
63
Mockito .clearInvocations (target );
65
64
}
66
65
66
+
67
67
protected abstract void assertProxyType (Object proxy );
68
68
69
+
69
70
private void invokeProxy () {
70
71
throwableSeenByCaller = catchThrowable (() -> Objects .requireNonNull (proxy ).doSomething ());
71
72
}
72
73
73
74
@ SuppressWarnings ("SameParameterValue" )
74
- private Answer <?> sneakyThrow (Throwable throwable ) {
75
+ private static Answer <?> sneakyThrow (Throwable throwable ) {
75
76
return invocation -> {
76
77
throw throwable ;
77
78
};
78
79
}
79
80
80
81
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
-
104
82
@ Nested
105
- class WhenThereIsOneInterceptor {
83
+ class WhenThereIsOneInterceptorTests {
106
84
107
85
@ Nullable
108
86
private Throwable throwableSeenByInterceptor ;
109
87
110
88
@ BeforeEach
111
89
void beforeEach () {
112
90
proxyFactory .addAdvice (captureThrowable ());
113
- proxy = (MyInterface ) proxyFactory .getProxy (ProxyExceptionHandlingTests . class .getClassLoader ());
91
+ proxy = (MyInterface ) proxyFactory .getProxy (getClass () .getClassLoader ());
114
92
assertProxyType (proxy );
115
93
}
116
94
117
95
@ Test
118
96
void targetThrowsUndeclaredCheckedException () throws DeclaredCheckedException {
119
- doAnswer (sneakyThrow (undeclaredCheckedException )).when (target ).doSomething ();
97
+ willAnswer (sneakyThrow (undeclaredCheckedException )).given (target ).doSomething ();
120
98
invokeProxy ();
121
99
assertThat (throwableSeenByInterceptor ).isSameAs (undeclaredCheckedException );
122
100
assertThat (throwableSeenByCaller )
@@ -126,15 +104,15 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
126
104
127
105
@ Test
128
106
void targetThrowsDeclaredCheckedException () throws DeclaredCheckedException {
129
- doThrow (declaredCheckedException ).when (target ).doSomething ();
107
+ willThrow (declaredCheckedException ).given (target ).doSomething ();
130
108
invokeProxy ();
131
109
assertThat (throwableSeenByInterceptor ).isSameAs (declaredCheckedException );
132
110
assertThat (throwableSeenByCaller ).isSameAs (declaredCheckedException );
133
111
}
134
112
135
113
@ Test
136
114
void targetThrowsUncheckedException () throws DeclaredCheckedException {
137
- doThrow (uncheckedException ).when (target ).doSomething ();
115
+ willThrow (uncheckedException ).given (target ).doSomething ();
138
116
invokeProxy ();
139
117
assertThat (throwableSeenByInterceptor ).isSameAs (uncheckedException );
140
118
assertThat (throwableSeenByCaller ).isSameAs (uncheckedException );
@@ -155,17 +133,17 @@ private MethodInterceptor captureThrowable() {
155
133
156
134
157
135
@ Nested
158
- class WhenThereAreNoInterceptors {
136
+ class WhenThereAreNoInterceptorsTests {
159
137
160
138
@ BeforeEach
161
139
void beforeEach () {
162
- proxy = (MyInterface ) proxyFactory .getProxy (ProxyExceptionHandlingTests . class .getClassLoader ());
140
+ proxy = (MyInterface ) proxyFactory .getProxy (getClass () .getClassLoader ());
163
141
assertProxyType (proxy );
164
142
}
165
143
166
144
@ Test
167
145
void targetThrowsUndeclaredCheckedException () throws DeclaredCheckedException {
168
- doAnswer (sneakyThrow (undeclaredCheckedException )).when (target ).doSomething ();
146
+ willAnswer (sneakyThrow (undeclaredCheckedException )).given (target ).doSomething ();
169
147
invokeProxy ();
170
148
assertThat (throwableSeenByCaller )
171
149
.isInstanceOf (UndeclaredThrowableException .class )
@@ -174,21 +152,21 @@ void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException {
174
152
175
153
@ Test
176
154
void targetThrowsDeclaredCheckedException () throws DeclaredCheckedException {
177
- doThrow (declaredCheckedException ).when (target ).doSomething ();
155
+ willThrow (declaredCheckedException ).given (target ).doSomething ();
178
156
invokeProxy ();
179
157
assertThat (throwableSeenByCaller ).isSameAs (declaredCheckedException );
180
158
}
181
159
182
160
@ Test
183
161
void targetThrowsUncheckedException () throws DeclaredCheckedException {
184
- doThrow (uncheckedException ).when (target ).doSomething ();
162
+ willThrow (uncheckedException ).given (target ).doSomething ();
185
163
invokeProxy ();
186
164
assertThat (throwableSeenByCaller ).isSameAs (uncheckedException );
187
165
}
188
166
}
189
167
190
168
191
- protected interface MyInterface {
169
+ interface MyInterface {
192
170
193
171
void doSomething () throws DeclaredCheckedException ;
194
172
}
@@ -202,11 +180,11 @@ public void doSomething() throws DeclaredCheckedException {
202
180
}
203
181
204
182
@ SuppressWarnings ("serial" )
205
- protected static class UndeclaredCheckedException extends Exception {
183
+ private static class UndeclaredCheckedException extends Exception {
206
184
}
207
185
208
186
@ SuppressWarnings ("serial" )
209
- protected static class DeclaredCheckedException extends Exception {
187
+ private static class DeclaredCheckedException extends Exception {
210
188
}
211
189
212
190
}
0 commit comments