2017-01-31 42 views
0

我正在運行此JUint測試方法,並在運行時收到異常NullPointerException。如何在這裏處理這個異常?JUnit中的NullPointerException

@Test 
public void testDivision1() { 
    System.out.println("testDivision");` 
    Operation instance = new Operation(100, 2, null); 
    int res = instance.calculate(); 
    assertEquals(50, res); 

} 
+0

'Operation'看起來像什麼? – ppasler

+0

public class操作{0} {0} {0}私人final int firstOperand; \t private final int secondOperand; \t private final EnumOp操作符; \t公共操作(INT firstOperand,INT第二操作數,操作者EnumOp){ \t \t this.firstOperand = firstOperand; \t \t this.secondOperand = secondOperand; \t \t this.operator = operator; \t} \t公衆詮釋計算(){ \t \t返回operator.calculate(firstOperand,第二操作數); \t} } –

+0

請使用該代碼更新您的問題。 – ppasler

回答

0

。當你穿過nullEnumOp operator這裏

Operation instance = new Operation(100, 2, null); 

我認爲這個操作失敗

return operator.calculate(firstOperand, secondOperand); 

您應設置測試有效EnumOp

+0

@Test \t公共無效testDivision1()拋出NullPointerException異常{ \t \t嘗試{ \t \t \t的System.out.println( 「testDivision」); \t \t \t Operation instance = new Operation(100,2,null); \t \t \t int res = instance.calculate(); \t \t \t assertEquals(50,res); (「error」);();}};}}}}} catch(null){ \t \t} \t} –

+0

有可能添加投擲並嘗試同時捕獲? –

+0

這是可能的,但在這裏沒有意義。你說'這種方法拋出NPE,但我會無論如何捕捉它'。想想,你想測試什麼?測試該部門是否正常工作?或者如果拋出異常? – ppasler

相關問題