Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to force Mockito to throw RemoteException in JUnit test
    primarykey
    data
    text
    <p>This is my code which I want to force to throw a Remote Exception:</p> <pre><code>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; } </code></pre> <p>Here is the JUnit test I am running with Mockito:</p> <pre><code>boolean exceptionThrown=false; Bicycle mockB = mock(Bicycle); mockB.setBicycle(null); stub(mockB.getBicycle()).toThrow(new RemoteException(){boolean exceptionThrown = true;}); assertTrue(exceptionThrown); </code></pre> <p>I keep receiving the following error:</p> <pre><code>Checked exception is invalid for this method! </code></pre> <p>Any help will be appreciated.</p> <p>Edit:</p> <p>Instead of</p> <pre><code>stub(mockB.getBicycle()).toThrow(new RemoteException(){boolean exceptionThrown = true;}); </code></pre> <p>I have also tried</p> <pre><code>doThrow(new RemoteException(){boolean exceptionThrown = true;}).when(mockB).getBicycle(); </code></pre> <p>and</p> <pre><code>Mockito.when(mockB.getBicycle()).thenThrow(new RemoteException(){boolean exceptionThrown=true;}); </code></pre> <p>Still no luck.</p> <p>Edit2 - gone one step further after fully understanding the API and using it correctly:</p> <pre><code>when(mockB.getBicycle()).thenThrow(new RuntimeException()); </code></pre> <p>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.</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload