Search code examples
javaunit-testingjunitjmockit

JMockit multiple exceptions as result for method call


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)


Solution

  • Write it like this:

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