如何使用JUnit Test注释断言我的异常消息?

Csh*_*hah 286 java testing annotations junit4 assertion

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

这就是我要的:

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

Jes*_*man 511

您可以使用@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");
}
Run Code Online (Sandbox Code Playgroud)

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

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

Ray*_*orm 36

我喜欢这个@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!");
  }
Run Code Online (Sandbox Code Playgroud)

  • (expected = RuntimeException.class) 并抛出 re; 没有必要;应该只有在 try 和 catch 断言中抛出异常的方法。 (2认同)

c_m*_*ker 28

你必须使用@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());
  }
}
Run Code Online (Sandbox Code Playgroud)

  • try/catch版本的问题,现在JUnit提供了`@Test(expected = ...)`和`ExpectedException`,是我在很多场合看到有人_忘记将调用放到`fail()`at `try` block_的结尾.如果没有通过代码审查,您的测试可能是误报并且总是通过. (13认同)
  • 我知道编写一个catch块并在其中使用assert但为了更好的代码可读性,我想用注释做. (5认同)

Joh*_*erg 17

在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());
}
Run Code Online (Sandbox Code Playgroud)

这也适用于JUnit 5但具有不同的导入:

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

...
Run Code Online (Sandbox Code Playgroud)

  • 由于 JUnit 4.13 中提供了“assertThrows”,因此这应该是可接受的答案 (4认同)
  • 就像这个解决方案。应该迁移到 JUnit 5。 (3认同)
  • 我已经在使用 4.13 `assertThrows`,但还不知道它 `return` 是后续检查的异常。+1,正是我需要的:-D (2认同)

Krz*_*slo 9

实际上,最好的用法是使用try/catch.为什么?因为您可以控制您期望异常的位置.

考虑这个例子:

@Test (expected = RuntimeException.class)
public void someTest() {
   // test preparation
   // actual test
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 通过使用特定于排列的小测试用例,可以减轻对代码更改的担忧.有时这是不可避免的,我们必须依赖catch/try方法,但如果经常发生这种情况,那么我们需要修改编写测试用例函数的方式. (2认同)

use*_*141 7

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

}
Run Code Online (Sandbox Code Playgroud)

然后对于我的单元测试,我需要的只是这段代码:

@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"
}
Run Code Online (Sandbox Code Playgroud)


Ash*_*oti 6

假设我们的doStuff类中有一个方法FooService。当参数中传递null标志时,此方法可能会引发 NullPointerException

public class FooService {

  public void doStuff(Boolean flag) {
    try{
      if(flag){
        // do stuff
      }
    }catch (Exception e){
      throw new RuntimeException("Unexpected error occurred", e);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

使用Junit 5

Junit 5assertThrows返回异常,可用于断言异常消息,如下例所示:-

import static org.junit.jupiter.api.Assertions.*;

@Test
public void doStuff_testThrownException(){   
    // null is passed, expected NullPointerException
    Throwable exception = assertThrows(RuntimeException.class, () -> fooService.doStuff(null)); 

    assertEquals("Unexpected error occurred", exception.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

使用 AssertJ

AssertJ 有各种断言来测试异常,其中之一是assertThatRuntimeException可以链接来withMessage测试异常消息

import static org.assertj.core.api.Assertions.*;

@Test
public void doStuff_testThrownException(){
  // null is passed, expected NullPointerException
  assertThatRuntimeException().isThrownBy(() -> fooService.doStuff(null))
          .withMessage("Unexpected error occurred");
}
Run Code Online (Sandbox Code Playgroud)

来源:CodingNConcepts


小智 5

我从不喜欢用 Junit 声明异常的方式。如果我在注释中使用“预期”,从我的角度来看,我们似乎违反了“给定,何时,然后”模式,因为“然后”位于测试定义的顶部。

此外,如果我们使用“@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");
}
Run Code Online (Sandbox Code Playgroud)