【问题标题】:How do I assert my exception message with JUnit Test annotation?如何使用 JUnit Test 注释断言我的异常消息?
【发布时间】:2011-01-29 00:29:14
【问题描述】:

我已经编写了一些带有 @Test 注释的 JUnit 测试。如果我的测试方法抛出一个已检查的异常,并且如果我想将消息与异常一起断言,有没有办法使用 JUnit @Test 注释来做到这一点? AFAIK,JUnit 4.7 不提供此功能,但是否有任何未来版本提供它?我知道在.NET 中你可以断言消息和异常类。在 Java 世界中寻找类似的功能。

这就是我想要的:

@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}

【问题讨论】:

  • 现在我想多了...你确定断言这个消息是个好主意吗?您的问题让我深入研究了 junit 源代码,似乎他们可以轻松添加此功能。他们没有的事实让我认为这可能不是一个好的做法。为什么在您的项目中断言消息很重要?
  • 好问题。假设一个包含 15 行代码的方法从 2 个不同的地方抛出相同的异常。我的测试用例不仅需要断言异常类,还需要断言其中的消息。在理想的世界中,任何异常行为都应该有自己的异常。如果是这样,我的问题就不会出现,但生产应用程序没有针对每个异常行为的独特自定义异常。
  • 附带说明 - PHPUnit 中有 @expectedExceptionMessage 注释。

标签: java testing annotations junit4 assertion


【解决方案1】:

你必须使用@Test(expected=SomeException.class)吗?当我们必须断言异常的实际消息时,这就是我们所做的。

@Test
public void myTestMethod()
{
  try
  {
    final Integer employeeId = null;
    new Employee(employeeId);
    fail("Should have thrown SomeException but did not!");
  }
  catch( final SomeException e )
  {
    final String msg = "Employee ID is null";
    assertEquals(msg, e.getMessage());
  }
}

【讨论】:

  • 我知道编写一个 catch 块并在其中使用断言,但为了更好的代码可读性,我想使用注释。
  • 另外,如果以“正确”的方式进行操作,您将不会收到这么好的信息。
  • try/catch 版本的问题是,现在 JUnit 提供了@Test(expected=...)ExpectedException,我曾多次看到有人忘记拨打fail()try 块的末尾。如果代码审查没有发现,您的测试可能会误报并始终通过。
  • 这就是为什么我不喜欢所有这些声明性的东西。这使得访问您想要的内容变得困难。
【解决方案2】:

您可以将@Rule 注释与ExpectedException 一起使用,如下所示:

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

@Test
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() throws Exception {
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("Employee ID is null");

    // do something that should throw the exception...
    System.out.println("=======Starting Exception process=======");
    throw new NullPointerException("Employee ID is null");
}

请注意,ExpectedException 文档中的示例(当前)是错误的 - 没有公共构造函数,因此您必须使用 ExpectedException.none()

【讨论】:

  • 注意:对于我来说,当 expectMessage 被指定为空字符串时,没有执行消息的比较
  • 我个人不想使用它,因为为一小部分方法创建字段是不好的做法。不是对响应的批评,而是对 JUnit 设计的批评。如果存在,OP 的假设解决方案会好得多。
  • @redDevil:expectedMessage 检查错误消息是否“包含”此函数中指定的字符串(如错误消息的子字符串)
  • expectMessage 带有字符串参数进行 String.contains 检查,对于异常消息的精确匹配,请使用 hamcrest 匹配器failure.expectMessage(CoreMatchers.equalTo(...))
  • ExpectedException.none() 自 Junit 4.13 起已弃用
【解决方案3】:

如果使用@Rule,异常集将应用于Test类中的所有测试方法。

【讨论】:

  • 使用 Jesse Merriman 响应,仅在调用 expectedEx.expect() 和 expectedEx.expectMessage() 的测试方法中检查异常。其他方法会使用expectedEx = ExpectedException.none()的定义,即不期望异常。
【解决方案4】:

我喜欢@Rule 的答案。但是,如果由于某种原因您不想使用规则。还有第三种选择。

@Test (expected = RuntimeException.class)
public void myTestMethod()
{
   try
   {
      //Run exception throwing operation here
   }
   catch(RuntimeException re)
   {
      String message = "Employee ID is null";
      assertEquals(message, re.getMessage());
      throw re;
    }
    fail("Employee Id Null exception did not throw!");
  }

【讨论】:

  • (expected = RuntimeException.class) 并抛出重新;不是必须的;。应该只有在 try 和 catch 断言中抛出异常的方法。
  • @janusz j:我个人更喜欢保留(expected...throw re; 行,但删除fail(... 行。你或其他人能告诉我为什么我的偏好是/不是一个好的做法吗?
  • 在 try catch 中,您可以随时随地捕获异常。当你有 for ex: 在不同的地方抛出相同的异常类型时,你不会知道它是在哪里抛出的。
  • @janusz j:谢谢,我明白了。换句话说,如果我的测试方法在try catch块之外有0行代码,就可以了吗?
【解决方案5】:

Raystorm 有一个很好的答案。我也不是规则的忠实粉丝。我做了类似的事情,除了我创建了以下实用程序类来帮助提高可读性和可用性,这首先是注释的一大优点。

添加这个实用程序类:

import org.junit.Assert;

public abstract class ExpectedRuntimeExceptionAsserter {

    private String expectedExceptionMessage;

    public ExpectedRuntimeExceptionAsserter(String expectedExceptionMessage) {
        this.expectedExceptionMessage = expectedExceptionMessage;
    }

    public final void run(){
        try{
            expectException();
            Assert.fail(String.format("Expected a RuntimeException '%s'", expectedExceptionMessage));
        } catch (RuntimeException e){
            Assert.assertEquals("RuntimeException caught, but unexpected message", expectedExceptionMessage, e.getMessage());
        }
    }

    protected abstract void expectException();

}

那么对于我的单元测试,我只需要这段代码:

@Test
public void verifyAnonymousUserCantAccessPrivilegedResourceTest(){
    new ExpectedRuntimeExceptionAsserter("anonymous user can't access privileged resource"){
        @Override
        protected void expectException() {
            throw new RuntimeException("anonymous user can't access privileged resource");
        }
    }.run(); //passes test; expected exception is caught, and this @Test returns normally as "Passed"
}

【讨论】:

    【解决方案6】:

    实际上,最好的用法是使用 try/catch。为什么?因为你可以控制你期望异常的地方。

    考虑这个例子:

    @Test (expected = RuntimeException.class)
    public void someTest() {
       // test preparation
       // actual test
    }
    

    如果有一天代码被修改并且测试准备会抛出 RuntimeException 怎么办?在这种情况下,实际测试甚至没有经过测试,即使它没有抛出任何异常,测试也会通过。

    这就是为什么使用 try/catch 比依赖注解要好得多。

    【讨论】:

    • 很遗憾,这也是我的回答。
    • 通过小的、特定于排列的测试用例减轻了对代码更改的担忧。有时这是不可避免的,我们不得不依赖于 catch/try 方法,但如果这种情况经常发生,那么我们很可能需要修改我们编写测试用例函数的方式。
    • 这是您的测试和/或代码的问题。您不期望一般的 RuntimeException,您期望特定的异常,或者至少是特定的消息。
    • 我以RuntimeException为例,将此异常替换为任何其他异常。
    【解决方案7】:

    导入catch-exception 库并使用它。它比ExpectedException 规则或try-catch 干净得多。

    他们的文档示例:

    import static com.googlecode.catchexception.CatchException.*;
    import static com.googlecode.catchexception.apis.CatchExceptionHamcrestMatchers.*;
    
    // given: an empty list
    List myList = new ArrayList();
    
    // when: we try to get the first element of the list
    catchException(myList).get(1);
    
    // then: we expect an IndexOutOfBoundsException with message "Index: 1, Size: 0"
    assertThat(caughtException(),
      allOf(
        instanceOf(IndexOutOfBoundsException.class),
        hasMessage("Index: 1, Size: 0"),
        hasNoCause()
      )
    );
    

    【讨论】:

      【解决方案8】:

      我喜欢 user64141 的回答,但发现它可以更概括。这是我的看法:

      public abstract class ExpectedThrowableAsserter implements Runnable {
      
          private final Class<? extends Throwable> throwableClass;
          private final String expectedExceptionMessage;
      
          protected ExpectedThrowableAsserter(Class<? extends Throwable> throwableClass, String expectedExceptionMessage) {
              this.throwableClass = throwableClass;
              this.expectedExceptionMessage = expectedExceptionMessage;
          }
      
          public final void run() {
              try {
                  expectException();
              } catch (Throwable e) {
                  assertTrue(String.format("Caught unexpected %s", e.getClass().getSimpleName()), throwableClass.isInstance(e));
                  assertEquals(String.format("%s caught, but unexpected message", throwableClass.getSimpleName()), expectedExceptionMessage, e.getMessage());
                  return;
              }
              fail(String.format("Expected %s, but no exception was thrown.", throwableClass.getSimpleName()));
          }
      
          protected abstract void expectException();
      
      }
      

      请注意,将“失败”语句留在 try 块中会导致相关的断言异常被捕获;在 catch 语句中使用 return 可以防止这种情况发生。

      【讨论】:

        【解决方案9】:

        在 JUnit 4.13 中,您可以:

        import static org.junit.Assert.assertEquals;
        import static org.junit.Assert.assertThrows;
        
        ...
        
        @Test
        void exceptionTesting() {
          IllegalArgumentException exception = assertThrows(
            IllegalArgumentException.class, 
            () -> { throw new IllegalArgumentException("a message"); }
          );
        
          assertEquals("a message", exception.getMessage());
        }
        

        这也适用于JUnit 5,但导入不同:

        import static org.junit.jupiter.api.Assertions.assertEquals;
        import static org.junit.jupiter.api.Assertions.assertThrows;
        
        ...
        

        【讨论】:

        • 喜欢这个解决方案。应该迁移到 JUnit 5。
        • 嘎啊啊啊啊。截至今天(2019 年秋季),4.13 仍处于测试阶段? mvnrepository.com/artifact/junit/junit
        • v4.13 不再处于测试状态(2020 年 1 月发布)
        • 由于 assertThrows 在 JUnit 4.13 中可用,这应该是公认的答案
        • 我已经在使用 4.13 assertThrows,但还不知道它returns 是后续检查的异常。 +1,正是我需要的:-D
        【解决方案10】:
        @Test (expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "This is not allowed")
        public void testInvalidValidation() throws Exception{
             //test code
        }
        

        【讨论】:

        • 有人能帮我理解为什么这个答案是-1
        • 问题是Junit,但你的答案是TestNG
        • @aasha 你的回答确实帮助了我。谢谢。
        【解决方案11】:

        我从不喜欢使用 Junit 断言异常的方式。如果我在注释中使用“expected”,从我的角度来看,我们似乎违反了“given, when, then”模式,因为“then”位于测试定义的顶部。

        另外,如果我们使用“@Rule”,我们必须处理这么多样板代码。因此,如果您可以为测试安装新库,我建议您查看 AssertJ(该库现在随 SpringBoot 提供)

        然后是不违反“given/when/then”原则的测试,使用 AssertJ 进行验证:

        1 - 例外是我们所期望的。 2 - 它还有一条预期的消息

        看起来像这样:

         @Test
        void should_throwIllegalUse_when_idNotGiven() {
        
            //when
            final Throwable raisedException = catchThrowable(() -> getUserDAO.byId(null));
        
            //then
            assertThat(raisedException).isInstanceOf(IllegalArgumentException.class)
                    .hasMessageContaining("Id to fetch is mandatory");
        }
        

        【讨论】:

          【解决方案12】:

          我更喜欢 AssertJ。

                  assertThatExceptionOfType(ExpectedException.class)
                  .isThrownBy(() -> {
                      // method call
                  }).withMessage("My message");
          

          【讨论】:

            猜你喜欢
            • 2018-09-26
            • 1970-01-01
            • 2012-02-06
            • 1970-01-01
            • 1970-01-01
            • 2017-03-09
            • 2023-03-15
            • 1970-01-01
            相关资源
            最近更新 更多