Unable to run testNG tests from maven

浪尽此生 提交于 2020-01-25 06:43:27

问题


I have a project that uses testng APIs to dynamically create testng.xml file, so using the main function of java my project works as expected, but if I run my project via maven using 'Run as->maven build' It looks like only the BeforeSuite and BeforeTest classes are running, but the actual tests aren't running. Here's my pom.xml file:

<build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
          <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.22.1</version>
    </dependency>
  </dependencies>
          <configuration>
          <forkMode>never</forkMode>
          </configuration>
        </plugin>
         <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <executions>
    <execution>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>main.MainRunner</mainClass>
    <arguments>
    </arguments>
  </configuration>
</plugin>
      </plugins>
    </pluginManagement>
  </build>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.14.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.0</version>
</dependency>
<dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.41.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>
   <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

  </dependencies>

Here's my MainRunner.java class:

package main;

import java.io.IOException;

import org.testng.TestNG;

import framework.CreateXml;

public class MainRunner {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        TestNG testng= new TestNG();
        CreateXml createAndRun = new  CreateXml(testng);
        createAndRun.createAndRun();

    }

}

This MainRunner class calls the 'createAndRun()' function that creates the testng.xml suite and runs the tests like this:

testng.setXmlSuites(mySuites);

        testng.setListenerClasses(listenerClasses);
        testng.setVerbose(3);
        testng.run();

And finally, here's the stacktrace:

[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ Automata ---
[WARNING] The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
[WARNING] useSystemClassloader setting has no effect when not forking
[INFO] Surefire report directory: D:\Git\automata\Automata\target\surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.testng.TestNGProvider
[INFO] Running TestSuite
Configuring TestNG with: TestNG60Configurator
Surefire suite started.....
In Before Test
Starting ChromeDriver 2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90) on port 45113
Only local connections are allowed.
Dec 20, 2018 11:57:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
new instance
Surefire suite ended.....
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 8.472 s - in TestSuite
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ Automata ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ Automata ---
[INFO] Installing D:\Git\automata\Automata\target\Automata-0.0.1-SNAPSHOT.jar to C:\Users\A\.m2\repository\com\sanrotech\com\Automata\0.0.1-SNAPSHOT\Automata-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Git\automata\Automata\pom.xml to C:\Users\A\.m2\repository\com\sanrotech\com\Automata\0.0.1-SNAPSHOT\Automata-0.0.1-SNAPSHOT.pom
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ Automata >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ Automata <<<
[INFO] 
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ Automata ---
[WARNING] 
java.lang.ClassNotFoundException: main.MainRunner
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
    at java.lang.Thread.run(Thread.java:748)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.582 s
[INFO] Finished at: 2018-12-20T23:57:30+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project Automata: An exception occured while executing the Java class. main.MainRunner -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project Automata: An exception occured while executing the Java class. main.MainRunner
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:154)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:146)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:290)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:194)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. main.MainRunner
    at org.codehaus.mojo.exec.ExecJavaMojo.execute(ExecJavaMojo.java:352)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    ... 20 more
Caused by: java.lang.ClassNotFoundException: main.MainRunner
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
    at java.lang.Thread.run(Thread.java:748)
[ERROR] 
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I wonder what am I missing here.


回答1:


Finally I found a solution, I just had to add

<classpathScope>test</classpathScope>

to my pom.xml file and now it's working as expected.




回答2:


Add this to your Pom.xml file,Where you have your the placed the testng source location src/main/java or src/test/java or src/test/resources

 <configuration>
 <suiteXmlFiles>
 <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
 </suiteXmlFiles>
 </configuration>


来源:https://stackoverflow.com/questions/53874321/unable-to-run-testng-tests-from-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!