I'm trying to test the following method
MainClass {
....
Client client;
WebTarget target;
boolean doLogin(MultivaluedMap<String, String> headers) {
client =getRestClient();
target = client.target(BASE_URL))
.path("v1/login");
MultivaluedMap<String, Object> castedHeaders = castMap(headers);//casts headers by entry.
Response loginRsp = target
.request().headers(castedHeaders).
post(Entity.entity(buildIusLoginEntity(),
MediaType.APPLICATION_JSON_TYPE));
if (loginRsp.getStatus() != HttpServletResponse.SC_OK) {
return false;
}
return true;
}
}
Using the following test class
@Test
public void testdoLoginNegative() {
MainClass m = spy(new MainClass());
Client mockClient = mock(Client.class);
WebTarget target = mock(WebTarget.class);
Response loginRsp = Response.status(500).build();
doReturn(mockClient).when(m).getRestClient();
when(mockClient.target(anyString()).path(anyString())).thenReturn(target);
when(target.request().headers(any(MultivaluedMap.class)).post(any(Entity.class))).thenReturn(loginRsp);// this line throws a Null pointer exception.
Assert.assertFalse(m.doIusLogin(getMockHeaders()));
}
However, my mock seems to show a null pointer exception as indicated in the source code. Any thoughts on what I could be doing wrong.. would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire