'Is it possible to include Event Listener class when running cucumber using io.cucumber.core.cli.Main in a Gradle task?

I'm trying to write a custom Gradle task that will run cucumber from CLI with specified cucumber options, for example: gradle clean cucumber -Dcucumber.tags="@Regression". But the problem is that the test is running on the TestNG framework and the main TestNGRunner.class looks like this:

package runners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.Listeners;
import rs.ananas.automation.utils.listeners.EventListener;

@CucumberOptions(
        features = "src/test/resources/features/",
        glue = {
                "rs.ananas.automation.common",
                "rs.ananas.automation.steps"
        },
        tags = "@Regression",
        plugin = {"pretty",
                "html:src/test/resources/reports/cucumber-report.html",
                "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
                "rs.ananas.automation.utils.listeners.StepEventListener"
        }
)
@Listeners(EventListener.class)
public class TestNGRunner extends AbstractTestNGCucumberTests { }

Notice the @Listeners(EventListener.class) annotation which specifies which listener the tests should be using. In EventListener class there are basic onTestStart, onTestFinished, onTestSkipped functions which are just logging data in the test results class, and when the test is finished data will be inserted in the database so the report can be generated afterward.

The problem is that I cannot find a way to tell a Gradle task to use this listener when running from cucumber CLI. Since the project is building with Jenkins CI there should be a way to pass the cucumber tags from CLI so I don't have to manually change every time @Regression tag in TestNGRunner.class when pushing the project to a Git repo. I hope you understand my point.

Gradle task looks like this:

task cucumber(type: Test) {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output

            args = [
                    '-g', 'rs.ananas.automation.common',
                    '-g', 'rs.ananas.automation.steps',
                    '-t', System.getProperty("cucumber.tags"),
                    '-p', 'com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:',
                    '-p', 'rs.ananas.automation.utils.listeners.StepEventListener'
            ]
        }
    }
}

Here is a full build.gradle info:

plugins {
    id 'java'
    id 'application'
    id "com.commercehub.cucumber-jvm" version "0.14"
}

apply plugin: 'java'

group 'ananas.com'
version '1.0-SNAPSHOT'

compileJava.options.encoding = "UTF-8"
compileTestJava.options.encoding = "UTF-8"

repositories {
    mavenCentral()
}

ext{
    jupiterVersion = '5.7.0'
    gsonVersion = '2.8.6'
    cucumberVersion = '6.9.1'
    // cucumberVersion = '4.8.1'
    seleniumVersion = '4.0.0'
    // seleniumVersion = '3.141.59'
    webdrivermanagerVersion = '4.2.2'
    restAssuredVersion = '4.3.3'
    gherkinVersion = '16.0.0'
    slf4jVersion = '1.7.30'
    assertJVersion = '3.19.0'
}

dependencies {
    // jupiter
    // testImplementation "org.junit.jupiter:junit-jupiter-api:$jupiterVersion"
    // testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$jupiterVersion"

    // gson
    implementation "com.google.code.gson:gson:$gsonVersion"

    // slf4j-simple
    implementation "org.slf4j:slf4j-simple:$slf4jVersion"

    // slf4j-api
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.33'

    // log4j-core
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.1'

    // log4j-api
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.1'

    // log4j-slf4j-impl
    implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.17.1'

    // assertj
    implementation "org.assertj:assertj-core:$assertJVersion"

    // cucumber
    implementation "io.cucumber:cucumber-core:$cucumberVersion"
    implementation "io.cucumber:cucumber-java:$cucumberVersion"
    implementation "io.cucumber:cucumber-jvm:$cucumberVersion"
    implementation "io.cucumber:cucumber-junit:$cucumberVersion"
    implementation group: 'io.cucumber', name: 'cucumber-testng', version: '6.9.1'
    // implementation group: 'io.cucumber', name: 'cucumber-testng', version: '4.8.1'

    // gherkin
    testImplementation "io.cucumber:gherkin:$gherkinVersion"

    // selenium
    implementation "org.seleniumhq.selenium:selenium-java:$seleniumVersion"
    implementation "io.github.bonigarcia:webdrivermanager:$webdrivermanagerVersion"

    // rest assured
    implementation "io.rest-assured:rest-assured:$restAssuredVersion"

    // assertions with assertJ
    implementation "org.assertj:assertj-core:$assertJVersion"

    // apache poi for working with excel
    implementation group: 'org.apache.poi', name: 'poi', version: '5.0.0'
    implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.0.0'

    // mysql connector
    implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26'

    // testng for executing tests
    testImplementation group: 'org.testng', name: 'testng', version: '7.4.0'

    // jackson
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.4'

    // lombok
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.20'

    // extent reports with cucumber
    implementation group: 'com.vimalselvam', name: 'cucumber-extentsreport', version: '3.1.1'

    // extent reports
    implementation group: 'com.aventstack', name: 'extentreports', version: '5.0.9'

    // extent reports cucumber6 adapter
    implementation group: 'tech.grasshopper', name: 'extentreports-cucumber6-adapter', version: '2.8.4'

    // apache pdfbox for working with pdf files
    implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.24'

    // mssql jdbc
    implementation group: 'com.microsoft.sqlserver', name: 'mssql-jdbc', version: '9.4.0.jre11'

    // awaitility
    implementation group: 'org.awaitility', name: 'awaitility', version: '4.1.0'

    // mail
    implementation group: 'com.sun.mail', name: 'javax.mail', version: '1.6.2'

    // logging
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.16.0'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.16.0'

    // apache commons mail
    implementation group: 'org.apache.commons', name: 'commons-email', version: '1.5'

    // zipping files
    implementation group: 'org.zeroturnaround', name: 'zt-zip', version: '1.14'
}

test {
    useTestNG() {
        suites 'src/test/testng.xml'
    }
}

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}


task cucumber(type: Test) {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output

            args = [
                    '-g', 'rs.ananas.automation.common',
                    '-g', 'rs.ananas.automation.steps',
                    '-t', System.getProperty("cucumber.tags"),
                    '-p', 'com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:',
                    '-p', 'rs.ananas.automation.utils.listeners.StepEventListener'
            ]
        }
    }
}

Is there a way to specify which Event Listener can be used during cucumber execution in a Gradle task? Thanks in advance!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source