JUnit测试中的路径问题,使用Visual Studio Code。

huangapple go评论86阅读模式
英文:

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&#39;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 = &quot;/home/$USER/Desktop/S2/src/test/test1.in&quot;;
private String generatedFilePath = Paths.get(&quot;&quot;).toAbsolutePath().toString().concat(&quot;/src/test/test1.in&quot;);

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

&quot;java.test.config&quot;: {
&quot;workingDirectory&quot;: &quot;${workspaceFolder}&quot;
}

then it should work... had the same issue yesterday and found this solution today... and it works JUnit测试中的路径问题,使用Visual Studio Code。

huangapple
  • 本文由 发表于 2020年4月3日 21:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61013094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定