raisercostin raisercostin - 1 year ago 106
Java Question

mockito better expected exception test using spy

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.

public class CheckExceptionsWithMockitoTest {

@Test(expected = RuntimeException.class)
public void testExpectedException1() {
A a = new A();

public void testExpectedException2() {
A a = new A();
try {
fail("no exception thrown");
} catch (RuntimeException e) {
assertThat(e.getMessage(), org.hamcrest.Matchers.containsString("cause1"));

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


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.

Using catchexception I created the test like this

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{
public void testExpectedException3() {
A a = new A();
//if more details to be analized are needed
(IllegalStateException) caughtException(),
containsString("is not allowed to add counterparties")),
//more asserts could come

Answer Source

Use catch-exception library, or I guess that the solution you are looking for is your second implementation.

@expected doesn't provide any way to assert on the thrown exception except for its class, so you can't avoit try/catching (not that much boiler plate code !)

Mockito doesn't provide something likes a verifyThrows method.

So you can trade try/catching for an additional library : using catch-exception, you'll be able to catch exception in a single line and have it ready for further assertion(s).

Sample source code

A a = new A();


        .hasMessageContaining("is not allowed to add counterparties")