【问题标题】:Testing IllegalArgumentException is not catching as expected测试 IllegalArgumentException 未按预期捕获
【发布时间】:2017-12-13 14:25:03
【问题描述】:

我有以下测试代码:

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

这段代码应该返回一个IllegalArgumentException,它按预期执行。但是,它将测试变为红色。这是堆栈跟踪:

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)

我应该收到这个例外,但我不能。在Enum 中测试错误值时,我遇到了同样的行为。

为什么它会让我的测试失败?是不是因为异常是从这样一个地方抛出的,无法以这种方式捕获?

编辑

addPlayer() 方法:

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

它之前会被检测到(因为在这种情况下我们不会到达return false),但我正在从以前的开发人员那里收回一些旧代码,从而为这个特定的用例编写测试用例。

【问题讨论】:

  • "ERROR addPlayer, Player failure" 这个消息是从哪里来的?
  • 是否有委托方法捕获异常,然后将其丢弃或包装在其他类型的异常中?贴出 addPlayer() 方法的代码。
  • "ERROR addPlayer" 来自 Hibernate,"Player failure" 是自定义消息。我会发布代码。
  • 故事的寓意是从不 catch(Exception e)它总是不正确的!
  • 嗯,我正在学习:)。自己逐步掌握漂亮的设计和编写良好的代码是多么艰难。

标签: java exception junit illegalargumentexception


【解决方案1】:

您在addPlayer() 方法中捕获了IllegalArgumentException(它是Exception 的子类型)。它永远不会达到测试方法。 你看到的不是测试方法抛出的实际异常,而是log.error("Player failure : ", e);的控制台输出。

【讨论】:

  • 我真笨。当然。初学者的错误。谢谢你的敏锐。
猜你喜欢
  • 1970-01-01
  • 2021-07-29
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多