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>