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

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

Path issues in JUnit testing with Visual Studio Code

问题

  1. 我在使用 VSC 中的 JUnit 生成包含绝对路径的测试时遇到了一些问题。
  2. 以下两个变量 writtenFilePath generatedFilePath 在我在终端中打印时都包含相同的字符串。然而,在 JUnit 测试中使用它们时会产生不同的结果。
  3. ```java
  4. private String writtenFilePath = "/home/$USER/Desktop/S2/src/test/test1.in";
  5. private String generatedFilePath = Paths.get("").toAbsolutePath().toString().concat("/src/test/test1.in");

在测试中使用 writtenFilePath 会产生预期的结果,而使用 generatedFilePath 则不会,因为测试无法访问该文件。

下面是在以下 JUnit 测试中的错误提示,测试了这两个路径字符串是否相等。

  1. @Test
  2. public void identicalFilePathsAreEqual() {
  3. assertEquals(writtenFilePath, generatedFilePath);
  4. }
  1. **org.junit.ComparisonFailure:**
  2. **expected:**
  3. /home/$USER/[Desktop/S]2/src/test/test1.in
  4. **but was:**
  5. /home/$USER/[.config/Code/User/workspaceStorage/c1a1302e27095d0a416037f60c4e3677/redhat.java/jdt_ws/S2_a43208c]2/src/test/test1.in

我尝试运行的测试:(对于 writtenFilePath,按预期工作;对于 generatedFilePath,测试失败)

  1. @Test
  2. public void sizeIsSixFromTest1() throws IOException {
  3. System.setIn(new ByteArrayInputStream(readLineByLineJava8(generatedFilePath).getBytes()));
  4. Lexer lex = new Lexer(System.in);
  5. assertEquals(6, lex.howManyTokens());
  6. }
  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m experiencing some problems with generating tests containing absolute paths with JUnit in VSC.
  4. 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.
  5. ```java
  6. private String writtenFilePath = &quot;/home/$USER/Desktop/S2/src/test/test1.in&quot;;
  7. 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.

  1. @Test
  2. public void identicalFilePathsAreEqual() {
  3. assertEquals(writtenFilePath,generatedFilePath);
  4. }
  1. **org.junit.ComparisonFailure:**
  2. **expected:**
  3. /home/$USER/[Desktop/S]2/src/test/test1.in
  4. **but was:**
  5. /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)

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

答案1

得分: 2

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

自定义测试配置

在VSC的首选项中搜索 java.test.config

打开 settings.json

设置 workspacefolder 如下:

  1. "java.test.config": {
  2. "workingDirectory": "${workspaceFolder}"
  3. }

然后应该就可以正常运行了……昨天我也遇到了相同的问题,今天找到了这个解决方案……而且它有效 😉

英文:

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

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

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:

确定