[英]How do I assert an IOException message with JUnit Test annotation?
當我將@Rule與ExpectedException 一起使用時,如下面的1st( 改編后的文章 )所示,在Class2testTest類的(*)處出現錯誤“ Unhandled Exception type”。
package class2test;
import java.io.IOException;
public class Class2test {
public Class2test() throws IOException{
throw new IOException("j");
}
}
package test;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import class2test.Class2test;
public class Class2testTest {
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public void test() {
expectedEx.expect(IOException.class);
expectedEx.expectMessage("j");
new Class2test(); //(*)
}
}
我不明白這一點: 修改后的帖子是可重現的(使用RuntimeException而不是IOException可以工作)。
您應該在方法的簽名中指定拋出的異常:
public void test() throws Exception {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.