首页 文章

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

提问于
浏览
232

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

这就是我要的:

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

9 回答

  • 30

    您可以将@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...
    }
    

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

  • 27

    我喜欢@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!");
      }
    
  • 416

    你必须使用 @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());
      }
    }
    
  • 2

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

    考虑这个例子:

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

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

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

  • 0

    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"
    }
    
  • 1

    在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;
    
    ...
    
  • 7

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

  • 6

    我喜欢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块中保留“fail”语句会导致捕获相关的断言异常;在catch语句中使用return可以防止这种情况 .

  • 4

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

相关问题