Can I test for multiple thrown exceptions in one test method?

Viewed 11608

I have a well specified interface and against that I write my JUnit tests:

public interface ShortMessageService {

     /**
     * Creates a message. A message is related to a topic
     * Creates a date for the message
     * @throws IllegalArgumentException, if the message is longer then 255 characters.
     * @throws IllegalArgumentException, if the message ist shorter then 10 characters.
     * @throws IllegalArgumentException, if the user doesn't exist
     * @throws IllegalArgumentException, if the topic doesn't exist
     * @throws NullPointerException, if one argument is null.
     * @param userName
     * @param message
     * @return ID of the new created message
     */
     Long createMessage(String userName, String message, String topic);

[...]

}

As you can see the implementation can throw various exceptions for which I have to write tests. My current approach is to write one test method for one possible exception specified in the interface like this:

public abstract class AbstractShortMessageServiceTest
{

    String message;
    String username;
    String topic;

    /**
     * @return A new empty instance of an implementation of ShortMessageService.
     */
    protected abstract ShortMessageService getNewShortMessageService();

    private ShortMessageService messageService;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Before
    public void setUp() throws Exception
    {
        messageService = getNewShortMessageService();
        message = "Test Message";
        username = "TestUser";
        topic = "TestTopic";
    }

    @Test
    public void testCreateMessage()
    {
        assertEquals(new Long(1L), messageService.createMessage(username, message, topic));
    }

    @Test (expected = IllegalArgumentException.class)
    public void testCreateMessageUserMissing() throws Exception
    {
        messageService.createMessage("", message, topic);
    }

    @Test (expected = IllegalArgumentException.class)
    public void testCreateMessageTopicMissing() throws Exception
    {
        messageService.createMessage(username, message, "");
    }

    @Test (expected = IllegalArgumentException.class)
    public void testCreateMessageTooLong() throws Exception
    {
        String message = "";
        for (int i=0; i<255; i++) {
            message += "a";
        }
        messageService.createMessage(username, message, topic);
    }


    @Test (expected = IllegalArgumentException.class)
    public void testCreateMessageTooShort() throws Exception
    {
        messageService.createMessage(username, "", topic);
    }

    @Test (expected = NullPointerException.class)
    public void testCreateMessageNull() throws Exception
    {
        messageService.createMessage(username, null, topic);
    }

[...]

}

So for now I have to define a lot of test methods for that one method defined in the interface and that feels awkward. Can I combine all these exception tests in one test method or what is the best practice?

2 Answers
Related