I was trying to debug cucumber step definition in vscode but without luck.

The project was configured accordingly with an official manual Cucumber Java Tools. It compiles fine and shows cucumber output using command:

gradle cucumber

In order to attach to the daemon the following lines of code were added to gradle.properties:

org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005

It seems like vscode is attaching fine because I can see a call stack is popping up and down in vscode. It's even possible to break on "Caught Exceptions". But "custom" breakpoints are not triggered at all.

The following debug configuration is used in launch.json:

"type": "java",
"name": "Debug (Attach)",
"request": "attach",
"hostName": "localhost",
"port": 5005

Here is gradle.build:

plugins {
    id 'java'
}

repositories {
    flatDir {
        dirs 'libs'
    }
    jcenter()
    mavenCentral()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.google.guava:guava:27.1-jre'
    compile group: 'org.testng', name: 'testng', version: '6.14.3'

    testImplementation 'io.cucumber:cucumber-java:4.2.6'
}

configurations {
  cucumberRuntime {
    extendsFrom testImplementation
  }
}

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

sourceCompatibility = '11'
targetCompatibility = '11'
version = '1.2.1'

Notes:

  1. I've tried to attach to running gradle daemon using eclipse but it seems like it doesn't work too.

1 Answers

1
Pavel Sapehin On Best Solutions

Strangely, using the default cucumber's java runner doesn't allow Visual Studio Code nor Eclipse remote debugger to set a breakpoint on step definition.

But it's possible to solve this issue by using cucumber's junit4 runner. Here is an updated gradle configuration (notice, you don't need the "cucumber" task anymore):

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.google.guava:guava:27.1-jre'

    // used for running cucumber steps + powermock
    testCompile 'junit:junit:4.12'

    testCompile 'io.cucumber:cucumber-java:4.3.0'
    testCompile 'io.cucumber:cucumber-junit:4.3.0'
}

Note that junit:junit dependency also contains a junit runner. Then you can create an empty class, e.g.: JUnitRunnerWrapper that will contain cucumber's configuration (via annotations):

@RunWith(Cucumber.class)
@CucumberOptions(
  plugin = { "pretty", "html:build/reports/tests/cucumber-html-report" },
  glue = { "gradle.cucumber" },
  features =  "src/test/resources",
  monochrome = true)
public class JUnitRunnerWrapper { 
}

In order to make it work you have to install Java Test Runner for vscode. Then you'll be able to see the "Run Test/Debug Test" under the JUnitRunnerWrapper:

enter image description here

After pressing "Debug Test", vscode will launch the tests and breakpoints will be triggered:

enter image description here

Additional notes:

  1. You can still run the gradle task via gradle test command
  2. The output of the Run Test command can be shown using vscode Java: Show Test Output command