4242import java .io .IOException ;
4343import java .io .OutputStream ;
4444import java .lang .annotation .Annotation ;
45- import java .lang .reflect .Type ;
4645import java .net .URI ;
4746
4847import javax .ws .rs .core .GenericType ;
5756
5857import org .hamcrest .core .Is ;
5958import org .junit .Assert ;
59+ import org .junit .Before ;
6060import org .junit .Test ;
6161import 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 ;
6266import static org .junit .Assert .assertEquals ;
6367import static org .junit .Assert .assertFalse ;
6468import static org .junit .Assert .assertNull ;
6569import static org .junit .Assert .assertTrue ;
6670import 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 ;
7273
7374/**
7475 * {@code ClientRequest} unit tests.
7576 *
7677 * @author Marek Potociar (marek.potociar at oracle.com)
7778 */
78- @ RunWith (JMockit .class )
79+ @ RunWith (MockitoJUnitRunner .class )
7980public class ClientRequestTest {
8081
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+
8192 /**
8293 * Test of resolving properties in the client request.
8394 */
@@ -157,26 +168,27 @@ public void testResolveProperty() {
157168 assertEquals ("value-request" , request .resolveProperty ("name" , "value-default" ));
158169 }
159170
160- @ Test
161- public void testSSLExceptionHandling (@ Mocked MessageBodyWorkers workers , @ Mocked GenericType <?> entityType )
162- throws Exception {
171+ private ClientRequest mockThrowing (Exception exception ) throws IOException {
163172 JerseyClient client = new JerseyClientBuilder ().build ();
164173 final ClientRequest request = new ClientRequest (
165174 URI .create ("http://example.org" ),
166175 client .getConfiguration (),
167176 new MapPropertiesDelegate ());
168177
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 {
169189 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 );
180192
181193 try {
182194 request .doWriteEntity (workers , entityType );
@@ -186,26 +198,14 @@ OutputStream writeTo(Object entity, Class<?> rawType, Type type, Annotation[] an
186198 e , Is .is (ioException ));
187199 }
188200 }
201+
189202 @ Test
190- public void testRuntimeExceptionBeingReThrown (@ Mocked MessageBodyWorkers workers , @ Mocked GenericType <?> entityType )
203+ public void testRuntimeExceptionBeingReThrown ()
191204 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 ());
197205
198206 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 );
209209
210210 try {
211211 request .doWriteEntity (workers , entityType );
0 commit comments