Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JUnit测试注释断言异常消息?_Java_Testing_Annotations_Junit4_Assertion - Fatal编程技术网

Java 如何使用JUnit测试注释断言异常消息?

Java 如何使用JUnit测试注释断言异常消息?,java,testing,annotations,junit4,assertion,Java,Testing,Annotations,Junit4,Assertion,我已经用@Test注释编写了一些JUnit测试。如果我的测试方法抛出了一个选中的异常,并且如果我想将消息与异常一起断言,那么有没有办法使用JUnit@test注释来实现这一点?恕我直言,JUnit4.7不提供此功能,但将来的版本是否提供此功能?我知道在.NET中可以断言消息和异常类。在Java世界中寻找类似的特性 这就是我想要的: @Test (expected = RuntimeException.class, message = "Employee ID is null") public v

我已经用
@Test
注释编写了一些JUnit测试。如果我的测试方法抛出了一个选中的异常,并且如果我想将消息与异常一起断言,那么有没有办法使用JUnit
@test
注释来实现这一点?恕我直言,JUnit4.7不提供此功能,但将来的版本是否提供此功能?我知道在.NET中可以断言消息和异常类。在Java世界中寻找类似的特性

这就是我想要的:

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

您是否必须使用
@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());
  }
}
您可以使用注释,如下所示:

@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");
}
 @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");
}

请注意,
ExpectedException
文档中的示例(当前)是错误的-没有公共构造函数,因此必须使用
ExpectedException.none()
如果使用@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!");
  }

雷斯托姆回答得很好。我也不太喜欢规则。我做了一些类似的事情,除了创建以下实用程序类以帮助可读性和可用性,这是注释的一大优点

添加此实用程序类:

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

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

考虑这个例子:

@Test (expected = RuntimeException.class)
public void someTest() {
   // test preparation
   // actual test
}
如果有一天代码被修改,测试准备将抛出RuntimeException呢?在这种情况下,实际测试甚至没有被测试,即使它没有抛出任何异常,测试也会通过

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

导入库并使用它。它比
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()
  )
);

我喜欢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();

}
公共抽象类ExpectedThrowableAsserter实现Runnable{

在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());
}
这也适用于但与不同的导入:

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

...

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

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

然后进行不违反“给定/何时/然后”原则的测试,并使用AssertJ进行验证:

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

将如下所示:

@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");
}
 @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");
}

我更喜欢AssertJ

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

我知道编写catch块并在其中使用assert,但为了更好的代码可读性,我想对注释进行处理。此外,如果使用“正确”的方法,您也不会得到这样好的消息。try/catch版本的问题在于,现在JUnit提供了
@Test(预期=…)
ExpectedException
,我在很多场合看到有人忘记调用
fail()
试一下
块的末尾。如果没有被代码审查发现,你的测试可能是假阳性的,并且总是通过。这就是为什么我不喜欢所有这些声明性的东西。这会让你很难访问你想要的东西。现在我再仔细考虑一下……你确定断言消息是个好主意吗?你的问题让我很失望在junit源代码中添加了一点g,似乎他们可以很容易地添加此功能。事实上,他们没有添加,这让我认为这可能不是一个好的做法。为什么在您的项目中断言消息很重要?好问题。假设一个包含15行代码的方法从两个不同的位置引发相同的异常。我的测试用例不仅需要声明异常类,还需要声明其中的消息。在理想情况下,任何异常行为都应该有自己的异常。如果是这样,我的问题永远不会出现,但生产应用程序不会为每个异常行为都有其唯一的自定义异常。顺便说一句,应该有
@PHPUnit中的ExceptionMessage
注释。注意:对于我来说,当
expectMessage
被指定为空字符串时,消息的比较没有执行。我个人不想使用它,因为为方法的一小部分创建字段是不好的做法。这不是对响应的批评,而是对JUnit的批评设计。OP的假设解决方案如果存在的话会更好。@redDevil:expectedMessage检查错误消息是否“包含”此函数中指定的字符串(如错误消息的子字符串)带有string参数的expectMessage执行string.contains检查,若要精确匹配异常消息,请使用hamcrest matcher
失败。expectMessage(CoreMatchers.equalTo(…)
ExpectedException.none()
自Junit 4.13以来就被弃用了。遗憾的是,这也是我的答案。通过使用小的、排列特定的测试用例,可以缓解对代码更改的担忧。有时这是不可避免的,我们不得不依赖catch/try方法,但如果这种情况经常发生,那么我们可能需要修改编写测试用例函数的方式ons。这是您的测试和/或代码的问题。您不希望出现一般运行时异常,您希望出现特定异常,或者至少出现特定消息。我使用
RuntimeException
作为示例,将此异常替换为任何其他异常。使用Jesse Merriman响应