Inspecting actual exception contents

32 views
Skip to first unread message

ru...@windofkeltia.com

unread,
Mar 8, 2013, 12:55:18 PM3/8/13
to catch-excep...@googlegroups.com, Russell Bateman
In this code which works...

    /**
     * This is nothing more than a forced demonstration of <tt>catch-exception</tt> and FEST.
     */
    @Test
    public void throwException() throws AppException
    {
        UasAddressClient client = mock( UasAddressClient.class );
        ServiceLocator.set( UasAddressClient.class, client );
        AddressManager manager = new AddressManager();

        verifyException( manager, AppException.class ).throwException();

        when( manager ).throwException();
        assertThat( caughtException() )
                .isInstanceOf( AppException.class )
                .hasMessage( "Phooey!" )
                .hasNoCause();

        ServiceLocator.remove( UasAddressClient.class );
    }

...what I really need to do (in addition to noticing the exception) is verify that AppException.getHttpStatus() returns various Status values.

How do I accomplish this? Or, how is this wrong-headed. I have been doing it just fine using try...catch in my JUnit code, but I'm warned by Tomek's book that this isn't how it's best done.

Thanks,

Russ

rw...@gmx.de

unread,
Mar 8, 2013, 1:43:34 PM3/8/13
to catch-excep...@googlegroups.com, Russell Bateman
>  verifyException( manager, AppException.class ).throwException();

This line redundant because you call the method under test in the following line.


> when( manager ).throwException();
> assertThat( caughtException() )

The first of the two lines uses BDD/FEST syntax, and the second line uses a Hamcrest matcher. You don't have to mix these coding styles.

The following complete example shows the differences between the coding styles. Each test uses its own coding style. Choose the coding style you prefer. Personally, I prefer the first one, because it is very easy to understand.

package com.googlecode.catchexception.help;

import static com.googlecode.catchexception.CatchException.catchException;
import static com.googlecode.catchexception.CatchException.caughtException;
import static com.googlecode.catchexception.CatchException.verifyException;
import static com.googlecode.catchexception.apis.CatchExceptionBdd.then;
import static com.googlecode.catchexception.apis.CatchExceptionBdd.when;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.hasMessage;
import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.hasNoCause;
import static org.hamcrest.CoreMatchers.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class AddressManagerTest {

    private static class AppException extends RuntimeException {

        public AppException() {
            super("Phooey!");
        }
    };

    private static class AddressManager {

        public void throwException() {
            throw new AppException();
        }
    }

    private AddressManager manager = new AddressManager();

    @Test
    public void testThrowException_usingStandardAPI_variant1()
            throws AppException {

        catchException(manager).throwException();
        assertTrue(caughtException() instanceof AppException);
        assertEquals("Phooey!", caughtException().getMessage());
        assertNull(caughtException().getCause());
    }

    @Test
    public void testThrowException_usingStandardAPI_variant2()
            throws AppException {

        verifyException(manager, AppException.class).throwException();
        assertEquals(caughtException().getMessage(), "Phooey!");
        assertNull(caughtException().getCause());
    }

    @Test
    public void testThrowException_usingHamcrest() throws AppException {

        catchException(manager).throwException();
        assertThat(caughtException(), //
                allOf( //
                is(AppException.class), //
                        hasMessage("Phooey!"), //
                        hasNoCause() //
                ) //
        );
    }

    @Test
    public void testThrowException_usingFEST_API() throws AppException {

        when(manager).throwException();
        then(caughtException()) //
                .isInstanceOf(AppException.class) //
                .hasMessage("Phooey!") //
                .hasNoCause();
    }

}

Message has been deleted

rw...@gmx.de

unread,
Mar 8, 2013, 1:50:05 PM3/8/13
to catch-excep...@googlegroups.com, Russell Bateman
...what I really need to do (in addition to noticing the exception) is verify that AppException.getHttpStatus() returns various Status values.

Just add the following lines.

  AppException caughtException = (AppException) caughtException();
  assertEquals(expectedState, caughtException.getHttpStatus());

Cheers
Rod
Reply all
Reply to author
Forward
0 new messages