[英]Why I cannot use expected in JUnit 4
Im trying to test method responsible for retreiving data from file. 我正在尝试测试负责从文件中检索数据的方法。 I want to test if exception is thrown properly.
我想测试是否正确抛出了异常。
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);
}
}
My tests: 我的测试:
package contentfile;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
class ContentFileRetrieverServiceTest {
private ContentFileRetrieverService contentFileRetrieverService = new ContentFileRetrieverService();
@Test
void getContentFile() {
String pathFile = "src\\test\\java\\resources\\TestText.txt";
String[] testedContent = contentFileRetrieverService.getContentFile(pathFile);
String[] expected = {"Line1 a", "Line2 b c", "Line 3"};
assertArrayEquals(expected, testedContent);
}
@Test(expected = IllegalArgumentException.class)
void getContentFileWhenFileDoesNotExist() {
String pathFile = "unknown";
String[] testedContent = contentFileRetrieverService.getContentFile(pathFile);
}
}
pom,xml 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>
It wont compile as it cannot resolve method excepted
what I am making wrong? 它不会编译,因为它不能解析的方法
excepted
什么,我作出错误的? PS: Could you tell me if I test this method properly with these two methods? PS:您能告诉我是否可以用这两种方法正确测试此方法吗?
You're mixing JUnit 4 and JUnit 5. 您正在混合JUnit 4和JUnit 5。
The expected
element exists in the JUnit4 @Test
expected
元素存在于JUnit4 @Test
JUnit 5 offers more powerful assertThrows
instead JUnit 5提供了更强大的
assertThrows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.