Skip to content
Advertisement

ClassNotFoundException in cucumber framework

I am using Cucumber framework for mobile app testing. In pom.xml, I have given this below plugin to run TestClass.java – which has code for uploading the latest APK version of the app. Main method is present inside this TestClass. I need this to run before the actual test execution. So I have used exec plugin. I’m getting this error if I am running with pom.xml –> mvn clean test. ClassNotFoundExpection is always thrown with pom.xml, but the individual class runs perfectly.

pom.xml:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version>
       <executions>
    <execution>
            <id>installAPK</id>
            <phase>generate-test-sources</phase>
            <goals>
            <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includePluginDependencies>true</includePluginDependencies>
        <mainClass>org.com.package1.TestClass</mainClass>
    </configuration>
</plugin>

Console error:

java.lang.ClassNotFoundException: org.com.package1.TestClass
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:246)
    at java.lang.Thread.run(Thread.java:748)

I also tried changing the phase after test-compile. Still i am getting the same error. Someone pls help.

Answer

According to the exec-maven-plugin documentation, the default dependency scope for the execution is runtime. Please change it to test with the following configuration if the TestClass is part of the test sources.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>3.0.0</version>
  <executions>
    ...
  </executions>
  <configuration>
    ...
    <classpathScope>test</classpathScope>
  </configuration>
</plugin>
Advertisement