有人可以指向JUnit 4的示例launch.json文件,以便我可以从Visual Studio Code运行测试吗?我无法在网上找到这个例子,所有创建一个的尝试都失败了。
我可以从命令行手动运行测试。 (FWIW,我正在使用CentOS。)以下是我运行它们的方法:
cd /opt/ABBYY/FREngine12/Samples/Java/Hello_VSC/src/test/java
java -cp .:/opt/junit/junit-4.12.jar:/opt/junit/hamcrest-core-1.3.jar org.junit.runner.JUnitCore Hello.AppTest
我的Java项目设置为支持Maven(我不是真的使用Maven - 我只是用Maven设置它,因为如果没有它,Visual Studio Code中的Java调试将无法工作)。所以在.classpath文件中我添加了以下条目,这些条目应该将命令行调用中的.jar文件添加到代码路径中:
<classpathentry kind="lib" path="/opt/junit/junit-4.12.jar" />
<classpathentry kind="lib" path="/opt/junit/hamcrest-core-1.3.jar" />
当我尝试设置launch.json文件时,我试图做这样的事情:
{
"type": "java",
"name": "Test-<Hello_VSC>",
"request": "launch",
"cwd": "/opt/ABBYY/FREngine12/Samples/Java/Hello_VSC/src/test/java/",
"console": "internalConsole",
"stopOnEntry": false,
"mainClass": "Hello.AppTest",
"projectName": "Hello_VSC",
"args": "org.junit.runner.JUnitCore Hello.AppTest",
},
但是,我收到此错误消息:
Error: Main method not found in class Hello.AppTest, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
即使我将cwd更改为完全伪造的目录,我也会得到相同的错误消息。我不知道我的类型参数是否错误,我的cwd参数是错误的,或者是否是其他的。
有什么建议吗?
答案 0 :(得分:1)
如果您搜索tasks.json
(而非launch.json
),则会获得this example
/*
Example for quick Java compilation and unit tests in VS Code.
Works well with simple BlueJ projects.
Hit Ctrl+Shift+B to compile currently open file with javac.
Hit Ctrl+Shift+T to test currently open test class.
See red wiggles for compilation errors / failed assertions or click exclamation mark in the status bar.
Uses a few workarounds for individual commands per task and filename without extension.
This is written for Windows but it should be easy to adopt for Linux and Mac.
*/
{
"version": "0.1.0",
"isShellCommand": true,
"suppressTaskName": true,
"showOutput": "silent",
"windows": {
"command": "powershell",
"args": ["-Command"],
"tasks": [
{
// tests the currently open test class. java has to be in %PATH% and the jUnit-jar in %CLASSPATH%.
"taskName": "junit",
"args": ["$env:CLASSPATH += ';${fileDirname}'; $class = [System.IO.Path]::GetFileNameWithoutExtension('${fileBasename}'); java org.junit.runner.JUnitCore $class | Select-String -NotMatch -Pattern 'at (?!.+${fileBasename})'"],
"isTestCommand": true,
"problemMatcher": {
"owner": "java",
"fileLocation": ["relative", "${fileDirname}"],
"pattern": [
{
"regexp": "^(.*)$",
"message": 1
},
{
"regexp": "^\\s*at .+\\((.+):([0-9]+)\\)$",
"file": 1,
"line": 2
}
]
}
},
]
}
}
但是它在“0.1.0”中,所以你必须先convert it to 2.0.0。
答案 1 :(得分:1)
没关系 - 我弄清楚了。我想我已经用我的项目正确设置了所有内容,但没有意识到使用Visual Studio Code的Java扩展运行测试的正确方法是只需单击Explorer(文件)图标,然后展开“Test Explorer” “选项,浏览到要运行的任何测试,右键单击,然后选择您的测试选项。无需使用launch.json文件来执行此操作。