【发布时间】:2018-07-20 11:22:09
【问题描述】:
假设我的方法 myMethod 检查了一系列场景,然后相应地抛出异常而不处理这些异常。
public MyResponse myMethod(MyRequest req)
{
try
{
if(req.property1 == null || req.property1.isEmpty())
throw new Exception("Property 1 is null of empty");
if(req.property2 == null || req.property2.isEmpty())
throw new Exception("Property 2 is null of empty");
//if no problem is found then proceed ...
}
catch(Exception e)
{
//log error
throw e;
}
}
如何测试引发的异常或至少检查与异常一起发送的消息?
@Test
public void aNullOrEmptyProperty1CausesExceptionTest() throws Exception {
//..
String property1 = "";
req.setProperty1(property1);
//...
MyResponse response = target.myMethod(req);
//How to check for the exception?
}
感谢您的帮助
【问题讨论】:
-
就像你在
myMethod中所做的一样,你会想要catch它。 -
我需要赶上吗?