【问题标题】:How to test throws clause in the class to be tested using Mockito如何使用 Mockito 测试要测试的类中的 throws 子句
【发布时间】:2016-07-08 13:07:01
【问题描述】:

流程从 Controller.callMethod() 开始。它调用一个抛出 MyException 的方法。 MyException 由 Controller 中编写的异常处理程序处理:

public class Controller{

public Response callMethod() throws MyException {

   ClassToTest classToTest = new ClassToTest();
   return(classToTest.method());
   }


@ExceptionHandler(MyException.class)
public @ResponseBody
Response myException(HttpServletRequest req,
        HttpServletResponse res, MyException myException) {

    Response response = new Response();
    response.setResponseCode(myException.getErrorCode());       
    return response;
}

}


 public class ClassToTest {
    public Response method() throws MyException {
        Response response = anotherMethod();
        return response;
    }


    public String anotherMethod(){
        if(//something)
          throw new MyException(Constant.ERROR_CODE);    // I need to test this line
    else
        return //some response
    }
}


}

这是我的测试课:

public class Test {

@Test
public void testMethod(){
try{
    ClassToTest classtotest = new ClassToTest();
    Response response  = classtotest.method();  //I want to get response after getting MyException (Response created by the myException ExceptionHandler)
    assertEquals("SUCCESS", response.getResponseCode);
  } catch(Exception e){
     //After getting MyException the control comes here & thats the problem. assertEquals never get executed.
  } }
}

当行 Response response = classtotest.method();正在执行,然后控件将进入 Test 类的缓存块。我想获取使用 myException ExceptionHandler 创建的响应并测试其响应代码。谁能告诉我如何使用 Mockito 做到这一点?

【问题讨论】:

    标签: java junit mockito junit4 bdd


    【解决方案1】:

    您可以使用expected 属性,但如果您需要测试某些特定消息,请尝试以下操作:

     @Test
      public void shouldThrowSomeException() {
            String errorMessage = "";
            try {
                Result result = service.method("argument");
            } catch (SomeException e) {
                errorMessage = e.getMessage();
            }
            assertTrue(errorMessage.trim().equals(
                    "ExpectedMessage"));
      }
    

    【讨论】:

      【解决方案2】:

      为什么不用这个注解

      @Test(expected=MyException.class)
      
      and to assert that the exception has occurred while you're calling your method
      
      //use the mock and call another method that throws an error
      when(yourMock.anotherMethod()).thenThrow(new MyException(Constant.ERROR_CODE));
      

      【讨论】:

      • 这没有意义。当您希望被测代码抛出异常时,您不会模拟
      【解决方案3】:

      您需要使用 @Test 中的预期字段来通知 junit 测试用例哪个是预期的异常。

        @Test(expected=Expectedexception.class)
      public void testMethod(){
        ClassToTest classtotest = new ClassToTest();
        Response response  = classtotest.method(); 
        //your test case
      }
      

      【讨论】:

      • 顺便说一句,在这种情况下,您实际上只将new ClassToTest().method() 放入您的测试方法中。当您期望异常时,绝对不需要 delcare classtotest 或 response。
      猜你喜欢
      • 2021-03-06
      • 2010-11-08
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多