如何使用Mockito测试要测试的类中的throws子句

时间:2016-07-08 13:07:02

标签: java junit mockito junit4 bdd

流程从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&创建的Response。测试其响应代码。谁能告诉我如何使用Mockito做到这一点?

3 个答案:

答案 0 :(得分:2)

为什么不使用此注释

class Program
{
    private static WeakReference<EventHandler<EventArgs>> _noClosure;
    private static WeakReference<EventHandler<EventArgs>> _closure;

    static void Main(string[] args)
    {
        Init();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        EventHandler<EventArgs> target;
        Console.WriteLine(_closure.TryGetTarget(out target));
        Console.WriteLine(_noClosure.TryGetTarget(out target));
    }

    class C { public void Go() { } }

    private static void Init()
    {
        _noClosure = new WeakReference<EventHandler<EventArgs>>((sender, args) =>
        {
        });

        var s = new C();
        _closure = new WeakReference<EventHandler<EventArgs>>((sender, args) =>
        {
              s.Go();
        });
    }
}

答案 1 :(得分:2)

您可以使用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 :(得分:1)

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

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