这来自官方的《 JMockit教程》:

@Test
   public void doSomethingHandlesSomeCheckedException() throws Exception
   {
      new Expectations() {
         DependencyAbc abc;

         {
            abc.stringReturningMethod();
            returns("str1", "str2");
            result = new SomeCheckedException();
         }
      };

      new UnitUnderTest().doSomething();
   }

是否有可能陈述相反的意思,那就是多个结果和一个返回-我需要抛出2个异常,然后才返回一个好的值。我正在寻找这样的东西:
  abc.stringReturningMethod();
  returns(new SomeCheckedException(), new SomeOtherException(),"third");

这是行不通的,JMockit无法将这些异常强制转换为String(这是stringReturningMethod的返回类型)

最佳答案

像这样写:

    abc.stringReturningMethod();
    result = new SomeCheckedException();
    result = new SomeOtherException();
    result = "third";

10-06 09:12
查看更多