比如有如下方法:
[code="java"]
private Response getResponse(Request request) throws XXXException
{
if (request == null)
{
throw new XXXException();
}
//To Do
}
[/code]
如果要使用jUnit测试这种抛出异常的情况,应该怎么写?
比如有如下方法:
[code="java"]
private Response getResponse(Request request) throws XXXException
{
if (request == null)
{
throw new XXXException();
}
//To Do
}
[/code]
如果要使用jUnit测试这种抛出异常的情况,应该怎么写?
[code="java"] YourClazz instance = new YourClazz();
Method method = instance.getClass().getDeclaredMethod("getResponse",
Request.class);
method.setAccessible(true);
try {
method.invoke(instance, new Object[] { null });
Assert.assertTrue(false);
} catch (Exception e) {
if(e.getCause() instanceof XXXException){
Assert.assertTrue(true);
return;
}
e.getCause().printStackTrace();
}
Assert.assertTrue(false);[/code]
可以使用反射