Exception handling JUnit中的NullPointerException

Exception handling JUnit中的NullPointerException,exception-handling,nullpointerexception,Exception Handling,Nullpointerexception,我正在运行这个JUint测试方法,并且在运行时获取异常NullPointerException。这里怎么处理这个异常 @Test public void testDivision1() { System.out.println("testDivision");` Operation instance = new Operation(100, 2, null); int res = instance.calculate(); assertEquals(50, res

我正在运行这个JUint测试方法,并且在运行时获取异常
NullPointerException
。这里怎么处理这个异常

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

}

当您在此处传递
EnumOp运算符的
null

Operation instance = new Operation(100, 2, null);
我想这次行动失败了

return operator.calculate(firstOperand, secondOperand);

您应该为测试设置有效的
EnumOp

什么是
操作
看起来像什么?公共类操作{private final int firstOperand;private final int secondOperand;private final EnumOp操作符;公共操作(int firstOperand,int secondOperand,EnumOp操作符){this.firstOperand=firstOperand;this.secondOperand=secondOperand;this.operator=operator;}public int calculate(){return operator.calculate(firstOperand,secondOperand);}}请用该代码更新您的问题。@Test public void testDivision 1()抛出NullPointerException{try{System.out.println(“testDivision”);Operation instance=new Operation(100,2,null);int res=instance.calculate();assertEquals(50,res);}catch(NullPointerException e){System.out.println(“error”);}有可能添加抛出并同时尝试、捕获?有可能,但在这里没有意义。你说“这个方法抛出NPE,但我无论如何都会捕获它”。想想,你想测试什么?测试除法是否正确?或者是否抛出异常?是的,如果我不使用抛出,没有问题。实际上,我不会让温家宝添加抛出ws以及何时添加try、catch.Have查看以下内容:我的答案有用吗?然后接受它并暂时关闭该问题。