Search code examples
maventestngmaven-profiles

Project running for mvn test but not running for mvn test -P<<profile_name>>


I have gone through lot of articles and SO posts before posting this question. I have created 2 maven profiles, one for each environment. When I run my pom.xml through eclipse's Run As > Maven test, the tests are executed. But when I execute it through Run Configuration, for this purpose, I created a Run Configuration called "Test Project" which has maven goals set to "test" and profiles set to "staging", the execution is throwing

There was an error in the forked process [ERROR] com/beust/jcommander/ParameterException : Unsupported major.minor version 52.0 [ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process [ERROR] com/beust/jcommander/ParameterException : Unsupported major.minor version 52.0

If it's a java version issue, then it shouldn't have executed when I executed Run As > Maven test.

Here's my pom.xml

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.sample</groupId>
<artifactId>TestProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>TestProject</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<profiles>
    <profile>
        <id>staging</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20</version>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>testng2.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.uncommons/reportng -->
    <dependency>
        <groupId>org.uncommons</groupId>
        <artifactId>reportng</artifactId>
        <version>1.1.4</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

</dependencies>

Both testng.xml and testng2.xml are similar except for the parameter value. Here's my testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite verbose="0" name="Test Project suite">
     <parameter name="env" value="staging" /> 
    <test name="Test Project sample tests">
        <classes>
            <class name="com.sample.TestProject.AppTest" />
        </classes>
    </test>
</suite> 

Solution

  • Please add properties as shown below and then retry.

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    

    This would cause the JDK version required to be set to 1.8. If you don't provide this, then by default, Maven resorts to being 1.5 compliant.

    When you running via the command line (by using mvn test), this would be required. The error is a re-iteration of this problem.

    When you do a Run as > maven test I am guessing that the JDK that is provided by your IDE comes into effect (I dont have a way to technically confirm this theory).

    For additional context, you can refer to this stackoverflow post as well.