英文:
Path issues in JUnit testing with Visual Studio Code
问题
我在使用 VSC 中的 JUnit 生成包含绝对路径的测试时遇到了一些问题。
以下两个变量 writtenFilePath 和 generatedFilePath 在我在终端中打印时都包含相同的字符串。然而,在 JUnit 测试中使用它们时会产生不同的结果。
```java
private String writtenFilePath = "/home/$USER/Desktop/S2/src/test/test1.in";
private String generatedFilePath = Paths.get("").toAbsolutePath().toString().concat("/src/test/test1.in");
在测试中使用 writtenFilePath 会产生预期的结果,而使用 generatedFilePath 则不会,因为测试无法访问该文件。
下面是在以下 JUnit 测试中的错误提示,测试了这两个路径字符串是否相等。
@Test
public void identicalFilePathsAreEqual() {
assertEquals(writtenFilePath, generatedFilePath);
}
**org.junit.ComparisonFailure:**
**expected:**
/home/$USER/[Desktop/S]2/src/test/test1.in
**but was:**
/home/$USER/[.config/Code/User/workspaceStorage/c1a1302e27095d0a416037f60c4e3677/redhat.java/jdt_ws/S2_a43208c]2/src/test/test1.in
我尝试运行的测试:(对于 writtenFilePath,按预期工作;对于 generatedFilePath,测试失败)
@Test
public void sizeIsSixFromTest1() throws IOException {
System.setIn(new ByteArrayInputStream(readLineByLineJava8(generatedFilePath).getBytes()));
Lexer lex = new Lexer(System.in);
assertEquals(6, lex.howManyTokens());
}
<details>
<summary>英文:</summary>
I'm experiencing some problems with generating tests containing absolute paths with JUnit in VSC.
The following two variables writtenFilePath and generatedFilePath both contain the same identical String when I print them in the terminal. However they produce different outcomes when used in a JUnit test.
```java
private String writtenFilePath = "/home/$USER/Desktop/S2/src/test/test1.in";
private String generatedFilePath = Paths.get("").toAbsolutePath().toString().concat("/src/test/test1.in");
writtenFilePath produces the desired outcome when used in a test while generatedFilePath does not as the test cannot access the file.
Below is the error prompt from the following JUnit test, testing that the two Path Strings are equal.
@Test
public void identicalFilePathsAreEqual() {
assertEquals(writtenFilePath,generatedFilePath);
}
**org.junit.ComparisonFailure:**
**expected:**
/home/$USER/[Desktop/S]2/src/test/test1.in
**but was:**
/home/$USER/[.config/Code/User/workspaceStorage/c1a1302e27095d0a416037f60c4e3677/redhat.java/jdt_ws/S2_a43208c]2/src/test/test1.in
The test I'm trying to run: (Works as intended with the writtenFilePath, fails with the generatedFilePath)
@Test
public void sizeIsSixFromTest1() throws IOException {
System.setIn(new ByteArrayInputStream(readLineByLineJava8(generatedFilePath).getBytes()));
Lexer lex = new Lexer(System.in);
assertEquals(6,lex.howManyTokens());
}
答案1
得分: 2
访问 https://code.visualstudio.com/docs/java/java-testing
自定义测试配置
在VSC的首选项中搜索 java.test.config
打开 settings.json
设置 workspacefolder 如下:
"java.test.config": {
"workingDirectory": "${workspaceFolder}"
}
然后应该就可以正常运行了……昨天我也遇到了相同的问题,今天找到了这个解决方案……而且它有效 😉
英文:
Visit https://code.visualstudio.com/docs/java/java-testing
Customize test configurations
search in preferences in VSC for java.test.config
open settings.json
set workspacefolder with
"java.test.config": {
"workingDirectory": "${workspaceFolder}"
}
then it should work... had the same issue yesterday and found this solution today... and it works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论