简体   繁体   English

使用 Visual Studio Code 进行 JUnit 测试中的路径问题

[英]Path issues in JUnit testing with Visual Studio Code

I'm experiencing some problems with generating tests containing absolute paths with JUnit in VSC.我在 VSC 中使用 JUnit 生成包含绝对路径的测试时遇到了一些问题。

The following two variables writtenFilePath and generatedFilePath both contain the same identical String when I print them in the terminal.当我在终端中打印以下两个变量时,writtenFilePath 和 generatedFilePath 都包含相同的字符串。 However they produce different outcomes when used in a JUnit test.但是,在 JUnit 测试中使用时,它们会产生不同的结果。

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.当在测试中使用时,writtenFilePath 会产生所需的结果,而 generateFilePath 不会,因为测试无法访问文件。

Below is the error prompt from the following JUnit test, testing that the two Path Strings are equal.下面是下面JUnit测试的错误提示,测试两个Path Strings是否相等。

@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)我正在尝试运行的测试:(使用writtenFilePath按预期工作,使用生成的FilePath失败)

@Test
public void sizeIsSixFromTest1() throws IOException {
    System.setIn(new ByteArrayInputStream(readLineByLineJava8(generatedFilePath).getBytes()));
    Lexer lex = new Lexer(System.in);
    assertEquals(6,lex.howManyTokens());
}

Visit https://code.visualstudio.com/docs/java/java-testing访问https://code.visualstudio.com/docs/java/java-testing

Customize test configurations自定义测试配置

search in preferences in VSC for java.test.config在 VSC 的首选项中搜索 java.test.config

open settings.json打开设置。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;)那么它应该可以工作......昨天有同样的问题,今天找到了这个解决方案......并且它有效;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM