2

This is my code which I want to force to throw a Remote Exception:

transient Bicycle b=null;

public Bicycle getBicycle() {
    if(b==null) {
        try {
            b=new Bicycle(this);
        } catch (RemoteException ex) {
            Logger.getLogger(Bicycle()).log(Level.SEVERE, null, ex);
        }
    }
    return b;
}

Here is the JUnit test I am running with Mockito:

boolean exceptionThrown=false;
Bicycle mockB = mock(Bicycle);
mockB.setBicycle(null);
stub(mockB.getBicycle()).toThrow(new RemoteException(){boolean exceptionThrown = true;});

assertTrue(exceptionThrown);

I keep receiving the following error:

Checked exception is invalid for this method!

Any help will be appreciated.

Edit:

Instead of

stub(mockB.getBicycle()).toThrow(new RemoteException(){boolean exceptionThrown = true;});

I have also tried

doThrow(new RemoteException(){boolean exceptionThrown = true;}).when(mockB).getBicycle();

and

Mockito.when(mockB.getBicycle()).thenThrow(new RemoteException(){boolean exceptionThrown=true;});

Still no luck.

Edit2 - gone one step further after fully understanding the API and using it correctly:

when(mockB.getBicycle()).thenThrow(new RuntimeException());

I don't know how to make the assert now. I tried putting a boolean once the exception gets called but the assert cannot see the boolean.

Any ideas?

John Vasiliou
  • 977
  • 7
  • 27
  • 48
  • 2
    Does the constructor for `Bicycle` declare that it throws `RemoteException`? – Todd Mar 23 '13 at 02:49
  • Nope, I think I understand my problem now though, i have a slightly different one where I don't know how to get my assert correct. I shall make an edit. – John Vasiliou Mar 23 '13 at 02:54
  • Possible duplicate of [throw checked Exceptions from mocks with Mockito](http://stackoverflow.com/questions/3762047/throw-checked-exceptions-from-mocks-with-mockito) – Martin Schröder Mar 08 '16 at 10:34

1 Answers1

4

The getBicycle() method will never return a RuntimeException. The code itself is catching the RuntimeException and, when caught, writes to the logger. The method itself will either return the Bicycle or null.

You will need to rethink how you want the getBicycle method operates. It could re-throw the RuntimeException atfer logging if you want the RuntimeException to bubble through. But, based on how that's written, the RuntmeException will never make it out to the JUnit test

EdH
  • 4,918
  • 4
  • 24
  • 34
  • Thank you very much, explains a lot. Is there a possibility I could test if the Logger gets logged? – John Vasiliou Mar 23 '13 at 04:49
  • 1
    Yes, but you'd still have to restructure the code. If you made Logger a instance variable with a setter and a getter. The getter could create the logger if it doesn't already exist. The setter would just assign it. Then, the test could provide a mocked Logger which it would assign to the code before running. Then the test could check the Logger. – EdH Mar 23 '13 at 04:53
  • Brilliant, I better get learning then! Only learnt how to JUnit test yesterday, this is all so new to me so thanks for your patience. – John Vasiliou Mar 23 '13 at 04:55
  • It's a worthwhile cause. Once you start writing tests, you start to code differently. You start thinking not only about how your code works, but how it can be tested. I've personally found that to be very beneficial. Keep at it. – EdH Mar 23 '13 at 04:57
  • Never log an exception and rethrow it. It will just multiply the stacktraces in the log. – Jens Schauder Mar 24 '16 at 12:29