如何使用 JUnit Test 注释断言异常消息?
-
20-09-2019 - |
题
我已经编写了一些 JUnit 测试 @Test
注解。如果我的测试方法抛出一个已检查的异常,并且我想断言消息和异常,有没有办法使用 JUnit 来做到这一点 @Test
注解?AFAIK,JUnit 4.7 不提供此功能,但未来的版本是否提供此功能?我知道在 .NET 中您可以断言消息和异常类。在 Java 世界中寻找类似的功能。
这就是我要的:
@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}
解决方案
你可以使用 @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()
.
其他提示
我喜欢 @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!");
}
你有使用@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());
}
}
在的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;
...
其实,最好的用法是用try / catch语句。为什么?因为你可以控制你所期望的异常之处。
考虑这个例子:
@Test (expected = RuntimeException.class)
public void someTest() {
// test preparation
// actual test
}
如果一天代码被修改和测试制备将抛出RuntimeException?在这种情况下实际测试甚至没有测试,即使它不抛出任何异常测试将通过。
这就是为什么它是更好的使用try / catch语句,而不是依靠注解。
Raystorm有一个很好的答案。我不是规则的大风扇无论是。我这样做,只是我创建以下实用工具类,以帮助可读性和可用性,这是摆在首位的注释的大plus'es的一个类似的东西。
...添加这个工具类:
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"
}
如果使用@rule,异常集合被施加到在测试类的所有的测试方法。
我喜欢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语句内使用返回防止这一点。
导入追赶异常文库,并使用它。它比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()
)
);
@Test (expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "This is not allowed")
public void testInvalidValidation() throws Exception{
//test code
}