junit测试异常方法
问题描述:
如何junit测试此异常方法。在assertEquals
方法中提供什么。junit测试异常方法
public void sys(){
try
{
String s = null;
System.out.println(s.length());
}
catch(NullPointerException e)
{
e.printStackTrace();
}
System.out.println("Hello");
}
}
答
可以编写一个JUnit测试其期望一个异常
@Test(expected = NullPointerException.class)
public void sys() {
String s = null;
System.out.println(s.length());
}
公共无效sysTest(){ \t \t ExceptionHandling E =新ExceptionHandling(); \t \t assertEquals(“de”,“Hello”); –
顺便说一句,你的代码中有一个额外的右括号。 – Lucky
什么是sys方法应该做的? –