模拟抛出空指针异常。新来的模拟
Mock throwing a null pointer exception. New to mockito
我正在尝试测试以下方法
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;
}
}
使用以下测试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);
//NPE on next line.
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()));
}
但是,我的模拟似乎显示了源代码中指示的空指针异常。任何关于我可能做错了什么的想法.. 将不胜感激。
您尝试使用 深度存根 而不准备您的模拟对象。尝试
WebTarget target = mock(WebTarget.class, Mockito.RETURNS_DEEP_STUBS);
不同之处在于,没有深度存根的模拟 returns 方法调用的默认值(通常为 null)。如果你用 Mockito.when
包含这样的调用,这不会有任何效果,但如果你附加进一步的调用,这会导致 NullPointerException
s,例如在
target
.request() // return null
.headers(any(MultivaluedMap.class)) // NullPointerException
.post(any(Entity.class)))
不过我希望
mockClient
.target(anyString()) // should return null
.path(anyString())) // should throw NullPointerException
NullPointerException
也失败了,没有激活深度存根
Client mockClient = mock(Client.class, Mockito.RETURNS_DEEP_STUBS);
并不是说激活深度存根可能方便编写测试,但它会导致测试代码出现奇怪的行为。
我正在尝试测试以下方法
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;
}
}
使用以下测试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);
//NPE on next line.
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()));
}
但是,我的模拟似乎显示了源代码中指示的空指针异常。任何关于我可能做错了什么的想法.. 将不胜感激。
您尝试使用 深度存根 而不准备您的模拟对象。尝试
WebTarget target = mock(WebTarget.class, Mockito.RETURNS_DEEP_STUBS);
不同之处在于,没有深度存根的模拟 returns 方法调用的默认值(通常为 null)。如果你用 Mockito.when
包含这样的调用,这不会有任何效果,但如果你附加进一步的调用,这会导致 NullPointerException
s,例如在
target
.request() // return null
.headers(any(MultivaluedMap.class)) // NullPointerException
.post(any(Entity.class)))
不过我希望
mockClient
.target(anyString()) // should return null
.path(anyString())) // should throw NullPointerException
NullPointerException
也失败了,没有激活深度存根
Client mockClient = mock(Client.class, Mockito.RETURNS_DEEP_STUBS);
并不是说激活深度存根可能方便编写测试,但它会导致测试代码出现奇怪的行为。