I want to test this unirest method . I'am using powerMockito .
try {
HttpResponse<JsonNode> resp = Unirest
.post(url)
.header("Content-Type", "application/json")
.body(payload)
.asJson();
if(resp.getStatus()!=200) {
JSONObject error=new JSONObject();
error.put("error", resp.getBody());
error.put("status code from promote",resp.getStatus());
return error;
}
return resp.getBody().getObject();
} catch (UnirestException e) {
e.printStackTrace();
}
The problem is when I try to test it asJson() returns nullPointerException. This is my test .
@Before
public void setUp() throws UnirestException, UnsupportedEncodingException {
JSONObject payload=payloadForDockerPromote("","","","","","","","");
JSONObject test=new JSONObject();
JsonNode jsonNode = mock(JsonNode.class);
JSONObject jsonObject = mock(JSONObject.class);
PowerMockito.mockStatic(Unirest.class);
ReflectionTestUtils.setField(groupService, "baseServicesUrl", "http://foo");
when(Unirest.post("http://foo/stage/promotedocker")).thenReturn(requestWithBody);
when(requestWithBody.header("Content-Type", "application/json")).thenReturn(requestWithBody);
when(requestWithBody.body(payload)).thenReturn(requestBodyEntity);
when(requestBodyEntity.asJson()).thenReturn(httpResponse);
when(httpResponse.getBody()).thenReturn(jsonNode);
when(jsonNode.getObject()).thenReturn(jsonObject);
when(httpResponse.getStatus()).thenReturn(Integer.valueOf(200));
}
@Test
public void groupPromoteDockerTest() throws Exception{
GroupService myClass = Mockito.spy(new GroupService());
JSONObject m=myClass.promoteDockerImage("","","","","","","","");
assertEquals(m,null);
}
Have you ever faced the same problem ?
Aucun commentaire:
Enregistrer un commentaire