在 VSCode 中调试 Java 单元测试

问题描述 投票:0回答:1

我试图在运行 JUnit 测试时调试一个类,但是,我的配置似乎不正确:

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Maven: compile",
      "type": "shell",
      "command": "mvn",
      "args": ["compile"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "problemMatcher": ["$maven"],
      "detail": "Compile the project using Maven"
    },
    {
      "label": "Maven: test",
      "type": "shell",
      "command": "mvn",
      "args": ["test"],
      "group": {
        "kind": "test",
        "isDefault": true
      },
      "problemMatcher": ["$maven"],
      "dependsOn": "Maven: compile",
      "detail": "Run all tests using Maven"
    },
    {
      "label": "Maven: test XodusDatabaseTest",
      "type": "shell",
      "command": "mvn",
      "args": ["compile", "test", "-Dtest=XodusDatabaseTest"],
      "group": {
        "kind": "test",
        "isDefault": true
      },
      "problemMatcher": ["$maven"],
      "detail": "Run XodusDatabaseTest using Maven"
    },
    {
      "label": "Maven: debug XodusDatabaseTest",
      "type": "shell",
      "command": "mvn",
      "args": [
        "compile", 
        "test",
        "-Dtest=XodusDatabaseTest",
        "-Dmaven.surefire.debug"
      ],
      "problemMatcher": ["$maven"],
      "detail": "Debug XodusDatabaseTest using Maven"
    }
  ]
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "Debug Test XodusDatabaseTest",
      "request": "launch",
      "mainClass": "",
      "preLaunchTask": "Maven: debug XodusDatabaseTest",
      "cwd": "${workspaceFolder}",
      "vmArgs": "-Dfile.encoding=UTF-8",
      "console": "integratedTerminal",
      "debugServer": 5005 
    }
  ]
}

运行“调试测试...”卡住等待调试器:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Listening for transport dt_socket at address: 5005

我希望它继续进行单元测试并在 VScode 中设置的任何断点处停止。

java maven visual-studio-code
1个回答
0
投票

您可以尝试将

"mainClass": "your main class name"
添加到
launch.json
文件中。另外,您还可以使用
win+R
输入
cmd
,然后用命令输入
netstat -ano | findstr 5005
。检查端口是否被占用。如果被占用,可以打开资源管理器查看被占用的进程是否为关键进程。关闭并重试。您可以查看 Java debuggingtesting 的文档。

© www.soinside.com 2019 - 2024. All rights reserved.