【发布时间】:2016-04-27 12:48:12
【问题描述】:
我在 java 类中有方法:
@Context
UriInfo uriInfo;
public void processRequest(@QueryParam ("userId") @DefaultValue("") String userId)
{
String baseURI = uriInfo.getBaseUri().toString();
if(userId == null)
{
//UserIdNotFoundException is my custom exception which extends Exceptition
throw new UserIdNotFoundException();
}
}
当我在 junit 测试当 userId 参数为 Null 时期望 UserIdNotFoundException 的上述方法时,我收到以下断言错误:expected an instance of UserIdNotFoundException but <java.lang.NullPointerException> is java.lang.NullPointerException。
@Test
public void testProcessRequest_throws_UserIdNotFoundException()
{
expectedException.expect(UserIdNotFoundException.class);
processRequest(null);
}
我的自定义异常类:
public class UserIdNotFoundException extends Exception
{
public UserIdNotFoundException()
{
}
public UserIdNotFoundException(String message)
{
super(message);
}
}
【问题讨论】:
标签: java unit-testing exception nullpointerexception mockito