Visual Studio Code 中的 JUnit [英] JUnit in Visual Studio Code
问题描述
我在 Visual Studio Code 中运行 JUnit 时遇到了各种问题.在我创建的所有项目中,我遇到了两个主要问题,它们并不共存.
问题 1:创建并运行 Java 项目后,如果我尝试添加 JUnit 测试类,程序找不到该类/测试.我应该看到一个选项来运行或调试我的测试,但没有弹出这样的选项.此外,当我单击测试资源管理器选项卡时,我没有看到列出的测试类,但它在项目文件夹中.
问题 2: 我在将一个最初在 Eclipse 中开发的项目导入到 VS 代码中取得了一些成功.这并不理想.这样做时,我遇到的问题是 VS Code 在 JUnit 导入方面存在问题.有时它无法识别 @Test 注释,并且会尝试通过让我在程序中进行导入来解决导入问题,(例如:使用 assertSame 方法时,我必须编写:org.junit.Assert.assertEquals("stringName", v1.getName);
有没有人遇到过类似的问题,或者有没有人对如何在没有 Maven 或其他项目经理的情况下运行 Java 项目有所了解?我确信有一种方法可以使这项工作发挥作用,但我觉得我错过了显而易见的事情.
要在 Visual Studio Code 中进行 Java 单元测试,我建议您满足以下要求
- JDK 11
当您单击运行测试"时,您应该会看到尚未实施"留言
如果您无法运行它并获得结果,可能至少有两个不同的原因
1.
<块引用>无法解析导入org.junit
要解决这个问题,如果项目使用 Maven 来管理依赖项,那么请确保您已经安装了 Maven (如何安装Windows 10 中的 Maven?).然后确保您运行了
mvn install
或mvn package
,如此处所述一>.2.
<块引用>SomethingTest.java不在项目PROJECT_NAME的classpath下,只报语法错误
要修复它,将该文件夹添加到项目根目录中的 .classpath
<属性><属性名称=可选"值=真"/><属性名称=maven.pomderived"值=真"/><属性名称=测试"值=真"/></属性></classpathentry> I'm having all sorts of trouble running JUnit in Visual Studio Code. I have had two main problems that do not co-exist in each all the projects I've created.
Problem 1: After creating and running a Java project, if I attempt to add a JUnit test class, the program cannot find the class/test. I should see an option to run or debug my test, but no such option pops up. Also when I click on the test Explorer tab, I don't see the test class listed, but it is in the project folder.
Problem 2: I have had some success with importing a project first developed in eclipse into VS code. This is not really ideal. When doing this the issue I have is that VS Code has troubles with the JUnit imports. Sometimes it does not recognize the annotation @Test and will attempt to solve the import troubles by having me at the import within the program, (Example: When using assertSame method I would have to write:
org.junit.Assert.assertEquals("stringName", v1.getName);
Has anyone had similar problems, or does anyone have some insight on how to run a java project without a maven or another project manager? I'm sure there is a way to make this work, but I feel like I'm missing the obvious.
解决方案To do Java Unit Tests in Visual Studio Code I suggest you to have in place the following requirements
Once you have that and your newly created Java project, you should know always separate Production code from Test code. For that, simply create a
test
folder in the same location assrc
.Once you have you have that folder, create a
SomethingTest.java
file with something likeimport static org.junit.Assert.fail; import org.junit.Test; public class NgramTest { @Test void test() { fail("Not yet implemented"); } }
When you click "Run Test", you should get a "Not yet implemented" message
If you're not able to run that and get a result, can be from at least two different reasons
1.
The import org.junit cannot be resolved
To fix it, if the project uses Maven to manage the dependencies, then make sure you've got Maven installed (How to install Maven in Windows 10?). Then make sure you ran
mvn install
ormvn package
, as mentioned here.2.
SomethingTest.java is not on the classpath of the project PROJECT_NAME, only syntax errors are reported
To fix it, add that folder to .classpath present in the root of the project
<classpathentry kind="src" output="target/test-classes" path="test"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> <attribute name="test" value="true"/> </attributes> </classpathentry>
这篇关于Visual Studio Code 中的 JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!