Note that there are some explanatory texts on larger screens.

plurals
  1. POmockito better expected exception test using spy
    primarykey
    data
    text
    <p>How can I make the 3rd test to check for the existence of cause1 in the message of the exception? I also listed in the first two tests that have drawbacks. First is not checking for the message second needs a lot of boilerplate code.</p> <pre><code>public class CheckExceptionsWithMockitoTest { @Test(expected = RuntimeException.class) public void testExpectedException1() { A a = new A(); a.doSomethingThatThrows(); } @Test public void testExpectedException2() { A a = new A(); try { a.doSomethingThatThrows(); fail("no exception thrown"); } catch (RuntimeException e) { assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("cause1")); } } @Test public void testExpectedException3() { A a = new A(); A spyA = org.mockito.Mockito.spy(a); // valid but doesnt work // doThrow(new IllegalArgumentException()).when(spyA).doSomethingThatThrows(); // invalid but in the spirit of what i want //chekThrow(RuntimeException.class,containsString("cause1")).when(spyA).doSomethingThatThrows(); } } </code></pre> <p>I couldn't find in Mockito something that works but there is something that looks like could be possible (at the level of syntax) and capabilities.</p> <hr> <p>Using catchexception I created the test like this</p> <pre><code>import static com.googlecode.catchexception.CatchException.*; import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import org.junit.*; public class CheckExceptionsWithMockitoTest{ //... @Test public void testExpectedException3() { A a = new A(); verifyException(a,IllegalArgumentException.class) .doSomethingThatThrows(); //if more details to be analized are needed assertThat( (IllegalStateException) caughtException(), allOf( is(IllegalStateException.class), hasMessageThat( containsString("is not allowed to add counterparties")), hasNoCause())); //more asserts could come assertNotNull(a); } } </code></pre>
    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