我知道有一种方法是:
@Test
public void foo() {
try {
// execute code that you expect not to throw Exceptions.
} catch(Exception e) {
fail("Should not have thrown any exception");
}
}
还有更干净的方法吗?(可能使用了Junit的@Rule?)
我知道有一种方法是:
@Test
public void foo() {
try {
// execute code that you expect not to throw Exceptions.
} catch(Exception e) {
fail("Should not have thrown any exception");
}
}
还有更干净的方法吗?(可能使用了Junit的@Rule?)
当前回答
AssertJ可以处理这种情况:
assertThatNoException().isThrownBy(() -> System.out.println("OK"));
查看文档了解更多信息https://assertj.github.io/doc/#assertj-core-exception-assertions-no-exception
其他回答
虽然这篇文章已经发布6年了,但是Junit世界已经发生了很多变化。现在可以使用Junit5
org.junit.jupiter.api.Assertions.assertDoesNotThrow()
Ex:
public void thisMethodDoesNotThrowException(){
System.out.println("Hello There");
}
@Test
public void test_thisMethodDoesNotThrowException(){
org.junit.jupiter.api.Assertions.assertDoesNotThrow(
()-> thisMethodDoesNotThrowException()
);
}
希望它能帮助那些正在使用Junit5新版本的人
您可以期望通过创建规则不会抛出异常。
@Rule
public ExpectedException expectedException = ExpectedException.none();
这可能不是最好的方法,但它肯定能确保不会从正在测试的代码块抛出异常。
import org.assertj.core.api.Assertions;
import org.junit.Test;
public class AssertionExample {
@Test
public void testNoException(){
assertNoException();
}
private void assertException(){
Assertions.assertThatThrownBy(this::doNotThrowException).isInstanceOf(Exception.class);
}
private void assertNoException(){
Assertions.assertThatThrownBy(() -> assertException()).isInstanceOf(AssertionError.class);
}
private void doNotThrowException(){
//This method will never throw exception
}
}
AssertJ可以处理这种情况:
assertThatNoException().isThrownBy(() -> System.out.println("OK"));
查看文档了解更多信息https://assertj.github.io/doc/#assertj-core-exception-assertions-no-exception
你可以基于junit的断言创建你自己的任何类型的断言,因为这些断言是专门为创建用户定义的断言而设计的,其工作方式与junit的断言完全一样:
static void assertDoesNotThrow(Executable executable) {
assertDoesNotThrow(executable, "must not throw");
}
static void assertDoesNotThrow(Executable executable, String message) {
try {
executable.execute();
} catch (Throwable err) {
fail(message);
}
}
现在测试所谓的场景methodMustNotThrow,并以junit风格记录所有失败:
//test and log with default and custom messages
//the following will succeed
assertDoesNotThrow(()->methodMustNotThrow(1));
assertDoesNotThrow(()->methodMustNotThrow(1), "custom facepalm");
//the following will fail
assertDoesNotThrow(()->methodMustNotThrow(2));
assertDoesNotThrow(()-> {throw new Exception("Hello world");}, "message");
//See implementation of methodMustNotThrow below
一般来说,在任何场景中,只要调用fail(someMessage)是有意义的,就有可能立即使测试中的任何内容失败,而fail(someMessage)正是为此目的而设计的。例如,在try/catch块中使用它,如果在测试用例中抛出任何东西,就会失败:
try{methodMustNotThrow(1);}catch(Throwable e){fail("must not throw");}
try{methodMustNotThrow(1);}catch(Throwable e){Assertions.fail("must not throw");}
这是我们测试的方法的样本,假设我们有这样一个方法,在特定的情况下一定不会失败,但它可以失败:
void methodMustNotThrow(int x) throws Exception {
if (x == 1) return;
throw new Exception();
}
上述方法是一个简单的示例。但这适用于复杂的情况,在这种情况下,失败不是那么明显。 这里有进口:
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.jupiter.api.Assertions.*;