作为方法调用的结果,JMockit有多个异常

Que*_*ueg 5 java junit unit-testing jmockit

这来自官方的JMockit教程:

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

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

      new UnitUnderTest().doSomething();
   }
Run Code Online (Sandbox Code Playgroud)

是否有可能说明相反的情况,即多个结果和一个返回 - 我需要抛出2个异常,然后才返回一个好的值.我正在寻找的东西是这样的:

  abc.stringReturningMethod();
  returns(new SomeCheckedException(), new SomeOtherException(),"third");
Run Code Online (Sandbox Code Playgroud)

这不起作用,JMockit无法将这些异常转换为String(返回类型stringReturningMethod)

Rog*_*rio 6

写这样:

    abc.stringReturningMethod();
    result = new SomeCheckedException();
    result = new SomeOtherException();
    result = "third";
Run Code Online (Sandbox Code Playgroud)

  • 它不会将所有三个调用的结果设置为"third"吗? (2认同)