gpt4 book ai didi

java - 如何使用 JUnit Test 注释断言 IOException 消息?

转载 作者:行者123 更新时间:2023-11-30 08:08:52 25 4
gpt4 key购买 nike

当我使用@Rule时与 ExpectedException如下所示,在第一个( adapted post )中,我在 Class2testTest 类中的 (*) 处收到错误“未处理的异常类型”。

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(); //(*)
}

}

我不明白这个:adapted post是可重现的(使用 RuntimeException 而不是 IOException 它可以工作)。

最佳答案

您应该在方法的签名中指定抛出的异常:

public void test() throws Exception {

关于java - 如何使用 JUnit Test 注释断言 IOException 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com