JMockit multiple exceptions as result for method call

Queequeg picture Queequeg · Jan 14, 2013 · Viewed 7.1k times · Source

This is from the official JMockit Tutorial:

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

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

      new UnitUnderTest().doSomething();
   }

Is it possible to state the opposite, that is multiple results and one return - I need to throw 2 exceptions and only then return a good value. Something like this is what Im looking for:

  abc.stringReturningMethod();
  returns(new SomeCheckedException(), new SomeOtherException(),"third");

This doesn't work, JMockit can't cast those exceptions to String (which is the return type of stringReturningMethod)

Answer

Rogério picture Rogério · Jan 15, 2013

Write it like this:

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