简体   繁体   中英

Maven exception while running the TestNG testsuite from command prompt or jenkins

OS: Ubuntu 16.04 Here is my updated pom.xml

Now I am running following command mvn install -Pbizom - DsuiteXmlFile=src/test/java/suite/testng_OrderSanity.xml

I have tried with different log4j version but not working.

This is my updated pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
    <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>com.mobisy.bizom</groupId>
    <artifactId>bizom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>

            </plugins>
    </build>
    <properties>          
         <extentreports.version>2.40.2</extentreports.version>
    <project.build.sourceEncoding>
              UTF-8 
    </project.build.sourceEncoding>
    <project.reporting.outputEncoding>
            UTF-8 
    </project.reporting.outputEncoding>

</properties>
        <profiles>
            <profile>
                <id>bizom</id>             
 <properties>

       <maven.test.failure.ignore>true</maven.test.failure.ignore>
                <suiteXmlFile>src/test/java/suite/testng_OrderSanity.xml</suiteXmlFile>
            </properties>
            <build>
                    <plugins>
                        <plugin>

                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.19.1</version>
                            <configuration>
                                <properties>
                                    <property>
                                        <name>listener</name>            
                          <value>bizom.listener.CustomListener</value>
                                    </property>
                                </properties>
                 <suiteXmlFiles>
                            <suiteXmlFile>${suiteXmlFile}
                 </suiteXmlFile>
                                </suiteXmlFiles>
                   <argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
                                <threadCount>1</threadCount>
                                <classpathDependencyScopeExclude>runtime</classpathDependencyScopeExclude>
                                <useSystemClassLoader>false</useSystemClassLoader>
                                <printSummary>true</printSummary>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-shade-plugin</artifactId>
                            <version>3.1.0</version>
                        </plugin>
                         <plugin>
                        <artifactId>maven-failsafe-plugin</artifactId>
                             <version>2.19.1</version>
                             <executions>
                                 <execution>
                                     <goals>
                                         <goal>integration-test</goal>
                                         <goal>verify</goal>
                                     </goals>
                                 </execution>
                             </executions>
                         </plugin>
                    </plugins>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
            <version>1.4.01</version>
        </dependency>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8-beta4</version>
        </dependency>


        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-testng</artifactId>
            <version>2.19.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
            <type>jar</type>
            <version>15.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>com.relevantcodes</groupId>
            <artifactId>extentreports</artifactId>
            <version>${extentreports.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>2.19.1</version>
            <type>maven-plugin</type>
        </dependency>

        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.4.1</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.github.fge</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>2.2.6</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-api</artifactId>
            <version>2.19.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>maven-surefire-common</artifactId>
            <version>2.19.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</version>

        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>      
        </dependency>
    </dependencies>
</project>

But when I am running it through command prompt by below command:

**mvn install -Pbizom -DsuiteXmlFile=src/test/java/suite/testng_OrderSanity.xml**

Now facing new error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project bizom: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
    [ERROR] java.lang.NoClassDefFoundError: org/apache/log4j/Priority
    [ERROR] at java.lang.Class.getDeclaredConstructors0(Native Method)
    [ERROR] at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    [ERROR] at java.lang.Class.getConstructor0(Class.java:3075)
    [ERROR] at java.lang.Class.newInstance(Class.java:412)
    [ERROR] atorg.testng.internal.ClassHelper.newInstance(ClassHelper.java:65)
    [ERROR] at org.testng.TestNG.setListenerClasses(TestNG.java:695)
    [ERROR] at org.testng.TestNG.configure(TestNG.java:1534)
    [ERROR] at org.testng.TestNG.configure(TestNG.java:1707)
    [ERROR] at org.apache.maven.surefire.testng.conf.TestNGMapConfigurator.configure(TestNGMapConfigurator.java:52)
    [ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:277)
    [ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    [ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:121)
    [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
    [ERROR] Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
    [ERROR] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    [ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    [ERROR] asun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    [ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)


Now its taking 2.19.1 but still error is coming. I have tried with different log4j version but not working.

Your build is effectively using Surefire 2.17 :

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin: 2.17 :test

But you defined 2.19.1 as dependency:

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <type>maven-plugin</type>
    </dependency>

Thus version 2.17 is actually running but 2.19.1 classes are on your classpath and used at runtime, obviously that is not compatible. New methods were added betwween 2.19 that were not present in 2.17 .

You defined version 2.19.1 as plugins under pluginManagement , but it is not currently used in your build. If you want to use 2.19.1 in your current build your should use plugins and not pluginManagement . If you are not sure about the difference between pluginManagement and plugins , I strongly recommend you see the Maven doc

Putting maven-surefire-plugin config outside of pluginManagement and removing the dependency of type <type>maven-plugin</type> should to the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM