views:

879

answers:

1

To run them together there are few options available but I have chosen using different profiles for Junit and TestNG. But now problem is with excluding and including test cases.

Since if we add testNG dependency to main project in maven it will skip all Junit,I have decided to put it in separate profile. So I am excluding TestNG tests in default(main) profile from compiling using following entry in pom.xml :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.0.2</version>
        <configuration>
        <source>1.5</source>
        <target>1.5</target>
        <testExcludes>
            <testExclude>**/tests/**.*</testExclude>
            <testExclude>**/tests/utils/**.*</testExclude>
        </testExcludes>
    </configuration>
</plugin>

and same for surefire plugin. So it works fine with main profile and executes only Junit4 tests. But when I use testNG profile it wont execute testNG test even it wont compile them. I am using following profile to execute them.

<profile>
    <id>testNG</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <testIncludes>
                        <testInclude>**/tests/**.java</testInclude>
                        <testInclude>**/tests/utils/**.*</testInclude>
                    </testIncludes>
    </configuration>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <skip>false</skip>
     <includes>
      <include>**/**.class</include>
      <include>**/tests/utils/**.class</include>
     </includes>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>org.testng</groupId>
   <artifactId>testng</artifactId>
   <version>5.8</version>
   <scope>test</scope>
   <classifier>jdk15</classifier>
  </dependency>
 </dependencies>
</profile>

Anybody have any idea why it is not including them and compiling again ?

+2  A: 

The configuration for the compiler plugin excludes the TestNG types. The configuration from the profile is merged with the default configuration, so your effective compiler configuration is:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.5</source>
    <target>1.5</target>
    <testIncludes>
      <testInclude>**/tests/**.java</testInclude>
      <testInclude>**/tests/utils/**.*</testInclude>
    </testIncludes>
    <testExcludes>
      <testExclude>**/tests/**.*</testExclude>
      <testExclude>**/tests/utils/**.*</testExclude>
    </testExcludes>
  </configuration>
</plugin>

This means that your TestNG types aren't ever compiled and therefore aren't run.

If you specify the <excludes> section in the testNG profile it will override the default excludes and your TestNG types will be compiled and run. I can't remember if it will work with an empty excludes tag (i.e. <excludes/>), you may have to specify something like this to ensure the default configuration is overridden.

    <testExcludes>
      <testExclude>dummy</testExclude>
    </testExcludes>
Rich Seller
Thanks much Rich, I really appreciate your help. You saved my hours of time man!! I was looking around this since morning. Even tried creating different profiles for both and all.I wasn't aware of the fact that maven uses the default configuration and we need to override this. Thanks lot.
RN
You're welcome. It's worth noting that in general you can run **mvn help:effective-pom** (and add any profiles with -P as normal) you can see the resultant POM, that would have shown the merged compiler configuration.
Rich Seller