Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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 为什么我不能在JUnit4中使用expected_Java_Unit Testing_Junit - Fatal编程技术网

Java 为什么我不能在JUnit4中使用expected

Java 为什么我不能在JUnit4中使用expected,java,unit-testing,junit,Java,Unit Testing,Junit,我正在尝试测试负责从文件中检索数据的方法。 我想测试异常是否正确抛出 package contentfile; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class ContentFileRetrieverService implements ContentFileRetriever

我正在尝试测试负责从文件中检索数据的方法。 我想测试异常是否正确抛出

   package contentfile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class ContentFileRetrieverService implements ContentFileRetriever {

    @Override
    public String[] getContentFile(String pathName) {

        Stream<String> contentFileStream;
        try {
            contentFileStream = Files.lines(Paths.get(pathName));
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }

        return contentFileStream.toArray(String[]::new);
    }
}
pom,xml

<dependency>

    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

org.junit.jupiter
JUnitJupiter api
释放
测试
朱尼特
朱尼特
4.12
测试
它不会编译,因为它无法解析方法
异常
我做错了什么?
附言:

你能告诉我我是否用这两种方法正确地测试了这个方法吗?

你在混合JUnit4和JUnit5

JUnit4中存在
预期的
元素


JUnit 5提供了更强大的功能

该属性已命名;你拼错了。@Tman抱歉,这是意料之中的事。我重写了它,现在犯了这个错误。即使我编写
@Test(expected=IllegalArgumentException.class)
它仍然不起作用,但是我对
4.12
有依赖关系,那么我如何与JUnit 5混合呢?可能你不仅有这种依赖关系。也许你的IDE提供了一个。。。测试类从JUnit5导入
org.junit.jupiter.api.test
,从JUnit4导入
org.junit.Assert
。尝试查找对
org.junit.jupiter
group artifacts的依赖关系我添加了编辑和粘贴pom.xml的版本。你能检查一下吗?是的,
JUnitJupiter api
是对JUnit5的依赖。因此,您可以删除对JUnit4的依赖并接受断言。。。或者坚持使用JUnit4并使用
org.junit.Test
而不是
org.junit.jupiter.api.Test
<dependency>

    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>RELEASE</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>