3

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.

Yassine Badache
  • 1,810
  • 1
  • 19
  • 38
  • 1
    "ERROR addPlayer, Player failure" where does this message come from? – Michael Dec 13 '17 at 14:30
  • 1
    Is there a delegate method catching the exception, and throwing it away or wrapping it in some other type of exception? Post the code for the addPlayer() method. – Andrew S Dec 13 '17 at 14:30
  • "ERROR addPlayer" comes from Hibernate, "Player failure" is a custom message. I'll post the code. – Yassine Badache Dec 13 '17 at 14:36
  • Your `addPlayer` method catches all exceptions. If you pass in null and `update` throws an exception, I'd expect it to just return false, as that's what your code does. If your production code is as you want it to be, you should just expect that `addPlayer(null)` returns false without an exception. – Jon Skeet Dec 13 '17 at 14:58
  • 1
    the moral of the story is **never** `catch(Exception e)` it is always incorrect! –  Dec 13 '17 at 15:03
  • 1
    Well, I'm learning :). Taking steps by myself into mastering beautiful design and well-written code is as rough as it gets. – Yassine Badache Dec 13 '17 at 15:04
  • @YassineBadache - While you are here, you could consider using ExpectedException.html(http://junit.org/junit4/javadoc/4.12/org/junit/rules/ExpectedException.html) rule to ensure exceptions are thrown (and can check messages too). I have personally found it way more useful than the expected clause in the annotation. – Veera Dec 16 '17 at 05:27

1 Answers1

2

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);.

André Stannek
  • 7,773
  • 31
  • 52