Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Assertions.assertThrows返回void,而它必须返回Throwable | JUnit 5_Java_Assert_Junit5_Assertion_Throwable - Fatal编程技术网

Java Assertions.assertThrows返回void,而它必须返回Throwable | JUnit 5

Java Assertions.assertThrows返回void,而它必须返回Throwable | JUnit 5,java,assert,junit5,assertion,throwable,Java,Assert,Junit5,Assertion,Throwable,我正在使用JUnit5库,我尝试使用assertThrows并获取抛出的异常,以便获取异常的消息。我有这样一段代码: import org.junit.jupiter.api.Assertions; //import org.junit.Assert; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Arrays; import static org.junit.jupiter.a

我正在使用JUnit5库,我尝试使用assertThrows并获取抛出的异常,以便获取异常的消息。我有这样一段代码:

import org.junit.jupiter.api.Assertions;
//import org.junit.Assert;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

public class Tests {
    private static void execute() throws Exception {
        ArrayList<String> filter = ListString.filter(new ArrayList<String>(Arrays.asList("1", "2", "3")), "4");
    }

    @Test
    public void listStringTest() {
        // This part doesn't work 
        Throwable throwable = Assertions.assertThrows(Exception.class, Tests::execute);
        assertEquals("Exception texnt", throwable.getMessage());
    }
}
import org.junit.jupiter.api.Assertions;
//导入org.junit.Assert;
导入org.junit.jupiter.api.Test;
导入java.util.ArrayList;
导入java.util.array;
导入静态org.junit.jupiter.api.Assertions.*;
公开课考试{
私有静态void execute()引发异常{
ArrayList过滤器=ListString.filter(新的ArrayList(Arrays.asList(“1”、“2”、“3”)、“4”);
}
@试验
public void listStringTest(){
//这部分不行
Throwable-Throwable=Assertions.assertThrows(Exception.class,Tests::execute);
assertEquals(“Exception texnt”,throwable.getMessage());
}
}
我得到以下信息:

但是断言抛出返回
T extensed Throwable
,而不是
void


我在哪里犯了错误?

您的类路径中可能有一个非常旧的JUnit Jupiter版本

里程碑版本5.0.0-M3(2016年11月)将返回类型添加到
assertThrows
。类路径中的版本似乎比该版本旧


确保使用最新版本的JUnit。

我找到了解决方案。原来我使用了旧版本的
junitjupiter

我在
build.gradle
文件中有这样的依赖项:

dependencies {
    testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}
当我将它们更新为:


它按预期工作。

要么是文档错误,要么更可能是您没有使用您想要的版本。如果按ctrl键单击
assertThrows
,您将在哪里结束?
dependencies {
     testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.2'
}