16
16
package org .springframework .data .projection ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
+ import static org .mockito .ArgumentMatchers .*;
19
20
import static org .mockito .Mockito .*;
20
21
21
22
import java .util .Collection ;
30
31
import org .junit .runner .RunWith ;
31
32
import org .mockito .Mock ;
32
33
import org .mockito .junit .MockitoJUnitRunner ;
34
+ import org .springframework .core .convert .ConversionService ;
35
+ import org .springframework .core .convert .support .DefaultConversionService ;
33
36
34
37
/**
35
38
* Unit tests for {@link ProjectingMethodInterceptor}.
@@ -43,11 +46,13 @@ public class ProjectingMethodInterceptorUnitTests {
43
46
@ Mock MethodInterceptor interceptor ;
44
47
@ Mock MethodInvocation invocation ;
45
48
@ Mock ProjectionFactory factory ;
49
+ ConversionService conversionService = new DefaultConversionService ();
46
50
47
51
@ Test // DATAREST-221
48
52
public void wrapsDelegateResultInProxyIfTypesDontMatch () throws Throwable {
49
53
50
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor );
54
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor ,
55
+ conversionService );
51
56
52
57
when (invocation .getMethod ()).thenReturn (Helper .class .getMethod ("getHelper" ));
53
58
when (interceptor .invoke (invocation )).thenReturn ("Foo" );
@@ -58,7 +63,7 @@ public void wrapsDelegateResultInProxyIfTypesDontMatch() throws Throwable {
58
63
@ Test // DATAREST-221
59
64
public void retunsDelegateResultAsIsIfTypesMatch () throws Throwable {
60
65
61
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (factory , interceptor );
66
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (factory , interceptor , conversionService );
62
67
63
68
when (invocation .getMethod ()).thenReturn (Helper .class .getMethod ("getString" ));
64
69
when (interceptor .invoke (invocation )).thenReturn ("Foo" );
@@ -69,7 +74,7 @@ public void retunsDelegateResultAsIsIfTypesMatch() throws Throwable {
69
74
@ Test // DATAREST-221
70
75
public void returnsNullAsIs () throws Throwable {
71
76
72
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (factory , interceptor );
77
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (factory , interceptor , conversionService );
73
78
74
79
when (interceptor .invoke (invocation )).thenReturn (null );
75
80
@@ -79,20 +84,21 @@ public void returnsNullAsIs() throws Throwable {
79
84
@ Test // DATAREST-221
80
85
public void considersPrimitivesAsWrappers () throws Throwable {
81
86
82
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (factory , interceptor );
87
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (factory , interceptor , conversionService );
83
88
84
89
when (invocation .getMethod ()).thenReturn (Helper .class .getMethod ("getPrimitive" ));
85
90
when (interceptor .invoke (invocation )).thenReturn (1L );
86
91
87
92
assertThat (methodInterceptor .invoke (invocation )).isEqualTo (1L );
88
- verify (factory , times (0 )).createProjection ((Class <?>) anyObject (), anyObject ());
93
+ verify (factory , times (0 )).createProjection ((Class <?>) any (), any ());
89
94
}
90
95
91
96
@ Test // DATAREST-394, DATAREST-408
92
97
@ SuppressWarnings ("unchecked" )
93
98
public void appliesProjectionToNonEmptySets () throws Throwable {
94
99
95
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor );
100
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor ,
101
+ conversionService );
96
102
Object result = methodInterceptor
97
103
.invoke (mockInvocationOf ("getHelperCollection" , Collections .singleton (mock (Helper .class ))));
98
104
@@ -107,7 +113,8 @@ public void appliesProjectionToNonEmptySets() throws Throwable {
107
113
@ SuppressWarnings ("unchecked" )
108
114
public void appliesProjectionToNonEmptyLists () throws Throwable {
109
115
110
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor );
116
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor ,
117
+ conversionService );
111
118
Object result = methodInterceptor
112
119
.invoke (mockInvocationOf ("getHelperList" , Collections .singletonList (mock (Helper .class ))));
113
120
@@ -123,7 +130,8 @@ public void appliesProjectionToNonEmptyLists() throws Throwable {
123
130
@ SuppressWarnings ("unchecked" )
124
131
public void allowsMaskingAnArrayIntoACollection () throws Throwable {
125
132
126
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor );
133
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor ,
134
+ conversionService );
127
135
Object result = methodInterceptor .invoke (mockInvocationOf ("getHelperArray" , new Helper [] { mock (Helper .class ) }));
128
136
129
137
assertThat (result ).isInstanceOf (Collection .class );
@@ -138,7 +146,8 @@ public void allowsMaskingAnArrayIntoACollection() throws Throwable {
138
146
@ SuppressWarnings ("unchecked" )
139
147
public void appliesProjectionToNonEmptyMap () throws Throwable {
140
148
141
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor );
149
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor ,
150
+ conversionService );
142
151
143
152
Object result = methodInterceptor
144
153
.invoke (mockInvocationOf ("getHelperMap" , Collections .singletonMap ("foo" , mock (Helper .class ))));
@@ -154,7 +163,8 @@ public void appliesProjectionToNonEmptyMap() throws Throwable {
154
163
@ Test
155
164
public void returnsSingleElementCollectionForTargetThatReturnsNonCollection () throws Throwable {
156
165
157
- MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor );
166
+ MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor (new ProxyProjectionFactory (), interceptor ,
167
+ conversionService );
158
168
159
169
Helper reference = mock (Helper .class );
160
170
Object result = methodInterceptor .invoke (mockInvocationOf ("getHelperCollection" , reference ));
0 commit comments