Yassine Badache
Yassine Badache

Reputation: 1851

Testing IllegalArgumentException is not catching as expected

I have the following testing code:

@Test(expected = IllegalArgumentException.class)
public void addPlayerFailureTest()    {
    playerDAO.addPlayer(null);
}

This code is supposed to return an IllegalArgumentException, and it does as expected. However, it turns the test red. This is the stacktrace:

ERROR  addPlayer, Player failure: 
java.lang.IllegalArgumentException: attempt to create merge event with null entity
    at org.hibernate.event.MergeEvent.<init>(MergeEvent.java:60)
    at org.hibernate.event.MergeEvent.<init>(MergeEvent.java:43)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:688)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:692)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:235)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
    at com.sun.proxy.$Proxy32.merge(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
    at com.sun.proxy.$Proxy32.merge(Unknown Source)
    at fr.game.core.dao.AbstractJpaGenericDAO.update(AbstractJpaGenericDAO.java:58)
    at fr.game.core.dao.player.JPAJoueurDAO.addJoueur(JPAJoueurDAO.java:40)
    at fr.game.core.dao.player.JoueurDAOTest.addJoueurFailureTest(JoueurDAOTest.java:23)

I am supposed to receive this exception, yet I cannot. I met the same behavior when testing a wrong value in an Enum.

Why does it make my test fail ? Is it because the exception is thrown from such a place that it cannot be caught that way ?

EDIT

The addPlayer() method:

public boolean addPlayer(Player player) {
    try {
        update(player);
        return true;
    } catch (Exception e) {
        log.error("Player failure : ", e);
        return false;
    }
}

It would have been detected before (as we would not reach the return false in this case), but I am taking back some old code from former devs, thus writing test cases for this particular use case.

Upvotes: 3

Views: 1354

Answers (1)

Andr&#233; Stannek
Andr&#233; Stannek

Reputation: 7863

You are catching the IllegalArgumentException (which is a subtype of Exception) in your addPlayer() method. It never reaches the test method. What you see isn't the actual exception thrown by the test method, but the console output of log.error("Player failure : ", e);.

Upvotes: 2

Related Questions