Search code examples
gradlecucumbercucumber-jvm

Getting cucumberException in thread main java.lang.NoClassDefFoundError: org/testng/ITestContext while running cucumber task


I am new to cucumber-jvm. While running cucumber task I am getting this error.

:cucumberException in thread "main" java.lang.NoClassDefFoundError: org/testng/ITestContext at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetPublicMethods(Class.java:2902) at java.lang.Class.getMethods(Class.java:1615) at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:40) at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86) at cucumber.runtime.Runtime.<init>(Runtime.java:91) at cucumber.runtime.Runtime.<init>(Runtime.java:69) at cucumber.runtime.Runtime.<init>(Runtime.java:65) at cucumber.api.cli.Main.run(Main.java:35) at cucumber.api.cli.Main.main(Main.java:18) Caused by: java.lang.ClassNotFoundException: org.testng.ITestContext at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 11 more I am unable to find why I am getting this error. Any kind of help will be highly appreciated. Thanks in advance.

build.gradle file :

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

configurations {
    cucumberRuntime {
        extendsFrom testRuntime
    }
}

 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', 'com', 'src/test/resources']
         }
     }
}

dependencies {
    compile 'junit:junit:4.11'
    compile 'info.cukes:cucumber-java:1.2.4'
    compile 'info.cukes:cucumber-junit:1.2.4'
    compile 'org.seleniumhq.selenium:selenium-java:2.53.1'
}

Solution

  • The basic idea is not to call the cucumber cli manualy. This will be handled by the testing framework of your choice (junit in your case). So get rid of the special task for cucumber. This shall be handled by the default gradle test task. Please see the inital setup here: https://github.com/tobi-sh/test-gradle-cucumber.

    If you really want to seperate the bdd tests from your other unit test than create an own configuration for the bdd test and include them into your testing folders on demand.