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.
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);.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With