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"); 
    } 
} 
+0

公共无效sysTest(){ \t \t ExceptionHandling E =新ExceptionHandling(); \t \t assertEquals(“de”,“Hello”); –

+0

顺便说一句,你的代码中有一个额外的右括号。 – Lucky

+0

什么是sys方法应该做的? –

可以编写一个JUnit测试其期望一个异常

@Test(expected = NullPointerException.class) 
public void sys() { 
    String s = null; 
    System.out.println(s.length()); 
}