42
42
import java .io .IOException ;
43
43
import java .io .OutputStream ;
44
44
import java .lang .annotation .Annotation ;
45
- import java .lang .reflect .Type ;
46
45
import java .net .URI ;
47
46
48
47
import javax .ws .rs .core .GenericType ;
57
56
58
57
import org .hamcrest .core .Is ;
59
58
import org .junit .Assert ;
59
+ import org .junit .Before ;
60
60
import org .junit .Test ;
61
61
import org .junit .runner .RunWith ;
62
+ import org .mockito .Mock ;
63
+ import org .mockito .Mockito ;
64
+ import org .mockito .MockitoAnnotations ;
65
+ import org .mockito .runners .MockitoJUnitRunner ;
62
66
import static org .junit .Assert .assertEquals ;
63
67
import static org .junit .Assert .assertFalse ;
64
68
import static org .junit .Assert .assertNull ;
65
69
import static org .junit .Assert .assertTrue ;
66
70
import static org .junit .Assert .fail ;
67
-
68
- import mockit .Mock ;
69
- import mockit .MockUp ;
70
- import mockit .Mocked ;
71
- import mockit .integration .junit4 .JMockit ;
71
+ import static org .mockito .Matchers .any ;
72
+ import static org .mockito .Matchers .same ;
72
73
73
74
/**
74
75
* {@code ClientRequest} unit tests.
75
76
*
76
77
* @author Marek Potociar (marek.potociar at oracle.com)
77
78
*/
78
- @ RunWith (JMockit .class )
79
+ @ RunWith (MockitoJUnitRunner .class )
79
80
public class ClientRequestTest {
80
81
82
+ @ Mock
83
+ private MessageBodyWorkers workers ;
84
+ @ Mock
85
+ private GenericType <?> entityType ;
86
+
87
+ @ Before
88
+ public void initMocks () {
89
+ MockitoAnnotations .initMocks (this );
90
+ }
91
+
81
92
/**
82
93
* Test of resolving properties in the client request.
83
94
*/
@@ -157,26 +168,27 @@ public void testResolveProperty() {
157
168
assertEquals ("value-request" , request .resolveProperty ("name" , "value-default" ));
158
169
}
159
170
160
- @ Test
161
- public void testSSLExceptionHandling (@ Mocked MessageBodyWorkers workers , @ Mocked GenericType <?> entityType )
162
- throws Exception {
171
+ private ClientRequest mockThrowing (Exception exception ) throws IOException {
163
172
JerseyClient client = new JerseyClientBuilder ().build ();
164
173
final ClientRequest request = new ClientRequest (
165
174
URI .create ("http://example.org" ),
166
175
client .getConfiguration (),
167
176
new MapPropertiesDelegate ());
168
177
178
+ Mockito .doThrow (exception ).when (workers )
179
+ .writeTo (any (), same (entityType .getRawType ()), same (entityType .getType ()),
180
+ Mockito .<Annotation []>any (), Mockito .<MediaType >any (),
181
+ Mockito .<MultivaluedMap <String , Object >>any (), Mockito .<PropertiesDelegate >any (),
182
+ Mockito .<OutputStream >any (), Mockito .<Iterable <WriterInterceptor >>any ());
183
+ return request ;
184
+ }
185
+
186
+ @ Test
187
+ public void testSSLExceptionHandling ()
188
+ throws Exception {
169
189
final IOException ioException = new IOException ("Test" );
170
- new MockUp <MessageBodyWorkers >(workers ) {
171
- @ Mock
172
- OutputStream writeTo (Object entity , Class <?> rawType , Type type , Annotation [] annotations ,
173
- MediaType mediaType , MultivaluedMap <String , Object > httpHeaders ,
174
- PropertiesDelegate propertiesDelegate , OutputStream entityStream ,
175
- Iterable <WriterInterceptor > writerInterceptors )
176
- throws java .io .IOException , javax .ws .rs .WebApplicationException {
177
- throw ioException ;
178
- }
179
- };
190
+
191
+ final ClientRequest request = mockThrowing (ioException );
180
192
181
193
try {
182
194
request .doWriteEntity (workers , entityType );
@@ -186,26 +198,14 @@ OutputStream writeTo(Object entity, Class<?> rawType, Type type, Annotation[] an
186
198
e , Is .is (ioException ));
187
199
}
188
200
}
201
+
189
202
@ Test
190
- public void testRuntimeExceptionBeingReThrown (@ Mocked MessageBodyWorkers workers , @ Mocked GenericType <?> entityType )
203
+ public void testRuntimeExceptionBeingReThrown ()
191
204
throws Exception {
192
- JerseyClient client = new JerseyClientBuilder ().build ();
193
- final ClientRequest request = new ClientRequest (
194
- URI .create ("http://example.org" ),
195
- client .getConfiguration (),
196
- new MapPropertiesDelegate ());
197
205
198
206
final RuntimeException runtimeException = new RuntimeException ("Test" );
199
- new MockUp <MessageBodyWorkers >(workers ) {
200
- @ Mock
201
- OutputStream writeTo (Object entity , Class <?> rawType , Type type , Annotation [] annotations ,
202
- MediaType mediaType , MultivaluedMap <String , Object > httpHeaders ,
203
- PropertiesDelegate propertiesDelegate , OutputStream entityStream ,
204
- Iterable <WriterInterceptor > writerInterceptors )
205
- throws java .io .IOException , javax .ws .rs .WebApplicationException {
206
- throw runtimeException ;
207
- }
208
- };
207
+
208
+ ClientRequest request = mockThrowing (runtimeException );
209
209
210
210
try {
211
211
request .doWriteEntity (workers , entityType );
0 commit comments