Search code examples
gradlejunit5cucumber-jvm

Cannot run cucumber JUnit tests on Gradle


I am trying to run a Cucumber / Selenium project using IntelliJ Community edition and Gradle 5.5.1.

My folder structure is as follows:

ProjectRoot
|
src---main---java
|
src---test---java---packagename---stepdefinitions---Steps.java
        |
        -----resources---feature---application.feature

My TestRunner class is as follows:

@RunWith(Cucumber.class)
@CucumberOptions(
        plugin = {"pretty", "json:cucumber-report.json"},
        features = {"src/test/resources/feature"})
public class TestRunner {
}

When I try to run the TestRunner what I get is the following:

Testing started at 18:48 ...
> Task :cleanTest
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources UP-TO-DATE
> Task :testClasses
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [org.fifthgen.scanmaltatesting.TestRunner](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
5 actionable tasks: 3 executed, 2 up-to-date

This is my build.gradle:

plugins {
    id 'java'
}

group 'org.fifthgen'
version '1'

sourceCompatibility = 11
targetCompatibility = 11

repositories {
    mavenCentral()
}

wrapper.gradleVersion = '5.5.1'

def cucumberVersion = '4.7.2'
def junitVersion = '5.5.2'

dependencies {
    implementation 'org.seleniumhq.selenium:selenium-java:3.141.59'

    testImplementation "io.cucumber:cucumber-java:${cucumberVersion}"
    testImplementation "io.cucumber:cucumber-junit:${cucumberVersion}"

    testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
}

test {
    useJUnitPlatform()
    scanForTestClasses = false
}

When I run the test task using Gradle, I get

BUILD SUCCESSFUL in 0s
4 actionable tasks: 1 executed, 3 up-to-date
18:52:41: Task execution finished 'test'.

Without the Cucumber scenarios being run.


Solution

  • ˋRunWithˋ is a JUnit 4 mechanism. In order to use that with JUnit 5 platform mechanism you must include dependency on junit-vintage engine which allows running JUnit 4 tests on JUnit 5.

    Alternatively you could change to the Cucumber engine for JUnit 5. I’m not sure though if it has already been released.