2 回答
TA贡献1813条经验 获得超2个赞
我在使用时遇到了同样的错误junit-jupiter-api 5.3.2。一项快速研究表明:
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingSupplier;
@Test
public void testVoidMethod() {
// works
Executable supplier = () -> crash();
assertThrows(NullPointerException.class, supplier);
}
@Test
public void testMethodWithReturnTypeCalledByExecutable() {
// works
Executable supplier = () -> wrong();
assertThrows(NullPointerException.class, supplier);
}
@Test
public void testMethodWithReturnType() {
// fails
assertThrows(NullPointerException.class, () -> wrong());
}
@Test
public void testMethodWithReturnTypeCalledByThrowingSupplier() {
// fails
ThrowingSupplier<?> supplier = () -> wrong();
assertThrows(NullPointerException.class, supplier);
}
public void crash() {
throw new NullPointerException();
}
public Object wrong() {
throw new NullPointerException();
}
使用assertThrows带有ThrowingSupplier提出了一个NoSuchMethodError。
我更新到junit-jupiter-api 5.4.0. API 发生了变化。不再assertThrows有带有ThrowingSupplieras 参数的方法。只有那些接受了Executable。但是有一些新方法采用ThrowingSupplier命名的assertDoesNotThrow.
最后我更新到junit-jupiter-api 5.5.2.
在5.4版本说明提到的类似病例的bug修正。然而,这不是对NoSuchMethodError.
更新:
NoSuchMethodError如果我通过 Eclipse 的 JUnit 插件运行 JUnit 测试,则会抛出 。它使用org.junit.jupiter.api_5.1.0.v20180327-1502.jar而在pom.xmlJUnit 5.3.2 中被指定。在 JUnit 5.3.2 上使用 maven 运行测试成功。
这是我使用 Eclipse 的情况。我没有 IntelliJ IDEA。
TA贡献1779条经验 获得超6个赞
我正在用我正在编码的东西查找同样的问题,我通过将测试方法放入大括号中来解决我的问题,如下所示:
Assertions.assertThrows(IllegalArgumentException.class,
() -> {dbProperties.thisMethodShouldThrowException();} );
添加回答
举报