Maven throws error while running testng test cases

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-29 12:15:04

问题


I have steup Eclipse + Maven + TestNG and I intend to run Selenium Test cases.

This is my POM File:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MyGroupId</groupId>
<artifactId>TestSuite</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.32.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-testng</artifactId>
        <version>2.14.1</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>

Now when I try to run Maven test, I get following error:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
org.apache.maven.surefire.util.SurefireReflectionException:
java.lang.reflect.InvocationTargetException; nested exception is
java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at 
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.apache.maven.surefire.util.ReflectionUtils.instantiateOneArg(ReflectionUtils.java:130)
at org.apache.maven.surefire.booter.SurefireReflector.instantiateProvider(SurefireReflector.java:247)
at org.apache.maven.surefire.booter.ProviderFactory.createProvider(ProviderFactory.java:81)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:171)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
 Caused by: java.lang.NoSuchMethodError: org.apache.maven.surefire.providerapi.ProviderParameters.getRunOrderCalculator()Lorg/apache/maven/surefire/util/RunOrderCalculator;
at org.apache.maven.surefire.testng.TestNGProvider.<init>(TestNGProvider.java:67)
... 10 more

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

Can someone suggest me what's that I am missing here.

Thanks in advance.


回答1:


You have to define the maven-surefire-plugin in the pluginManagement section like the following:

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.14.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

To use testng in relationship with the maven-surefire-plugin you usually only need to add testng as a dependency nothing more.

Furthermore remove the dependency you've given:

<dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-testng</artifactId>
    <version>2.14.1</version>
</dependency>

Apart from the above this is looking more like an integration tests which is the job of maven-failsafe-plugin and not of maven-surefire-plugin.

<plugin>
  <artifactId>maven-failsafe-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

This means your tests (presumably integration tests based on the selenium dependency) can be run by using:

mvn verify



回答2:


Maybe will help somebody. Me helped - change version dependency of testng

It was:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.11</version>
    <scope>test</scope>
</dependency>

And has become:

 <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.10</version>
    <scope>test</scope>
</dependency>



回答3:


I also had an issue that resulted in this error, but for me it turned out to be missing file access rights for the account running the tests to the test folder.

Might be something to check out




回答4:


changing the testNG dependency version in pom.xml from 6.14.3 to 6.10 will help out in avoiding the exception cases.



来源:https://stackoverflow.com/questions/16300120/maven-throws-error-while-running-testng-test-cases

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