Junit4 and TestNG in one project with Maven - java

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 ?

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>

Simplest solution is like this
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
More info about this: Mixing TestNG and JUnit tests in one Maven module – 2013 edition

I didn't find any combined solution online for both Surefire and Failsafe. The POM file changes below worked for me.
The Surefire trick is done by explicitly specifying the child plugin for both JUnit and TestNG.
The Failsafe trick is done by defining two executions, each with an empty configuration, one for JUnit and the other for TestNG.
Both solutions are hacks that I found online. I think the link to the Surefire trick is in another answer to this question.
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.version}</version>
<executions>
<execution>
<id>test-testng</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<testNGArtifactName>none:none</testNGArtifactName>
</configuration>
</execution>
<execution>
<id>test-junit</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<junitArtifactName>none:none</junitArtifactName>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>

Related

Maven run unit tests without integration tests, and integration tests without unit tests

I have structure:
- test
- java
- com
- A
- service
- serviceB
- DefaultServiceBTest.java
- integration
- DefaultServiceBIntegrationTest.java
I want to run separately unit and integration tests. I am using maven surefire and failsafe plugins:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
<printSummary>true</printSummary>
<excludes>
<exclude>integration/*.java</exclude>
</excludes>
</configuration>
</plugin>
Since mvn test defaultly runs surefire:test having this configuration for maven-surefire works, and using mvn test only runs unit tests - in my case all tests that are not in integration folder.
However for failsafe i have this configuration:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.plugin.version}</version>
<configuration>
<includes>
<include>**/integration/*.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
Having this configuration for maven-failsafe and running mvn verify -Pfailsafe results in first running unit tests, and then integration tests. However i dont want the unit tests to be executed with this. What is wrong with the configuration?
Thanks for help!
First you should not separate your unit tests from integration tests by using different directories. Use the existing naming conventions. Unit Test *Test.java and integration tests *IT.java...
Use the following configuration in your 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.soebes.youtube.maven.episodes</groupId>
<artifactId>example-episode-2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Episode 2: Unit- and/or Integration Testing</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.23.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M8</version>
<configuration>
<skipTests>${skipUTs}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M8</version>
<configuration>
<skipTests>${skipITs}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
By using that you can run your unit tests via:
mvn test
You can run your unit- and your integration tests via:
mvn verify
If you like to run your integration tests only:
mvn verify -DskipUTs
For detail explanations you could check the following YT video.

aggregating surefire reports in MultiModule project

I have multimodule maven project I want to get aggregated surefire reports . I tried below approach but I dont see aggregated surefire report generated. I could see report generated for individual modules.
I have added below plugin configuration in indvidual Modules (packaging jar)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.22.0</version>
</dependency>
</dependencies>
<configuration>
<useManifestOnlyJar>false</useManifestOnlyJar>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<aggregate>true</aggregate>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
</plugins>
</reporting>
And below in root level pom (packaging type pom)
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>${version.plugin.surefire}</version>
<inherited>false</inherited>
<configuration>
<aggregate>true</aggregate>
<linkXRef>true</linkXRef>
</configuration>
</plugin>
</plugins>
</reporting>
Could some one please help where I am going wrong ?
Try with mvn surefire-report:report -Daggregate=true

AspectJ not working in maven project in IntelliJ

I have maven project that use aspectJ for auditing. I'm use Intellij idea. Here is my plugin config in pom file:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>${java.version}</source>
<target>${java.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.version}</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
</plugin>
When I clean package the aspectJ plugin says:
Join point 'method-execution(java.util.List com.test.getHistories(java.lang.String, java.lang.String))' in Type 'com.test.HistoryService' (HistoryService.java:18) advised by around advice from 'com.test.aspects.AuditLoggerAspect' (AuditLoggerAspect.java:88)
Join point 'method-execution(java.lang.String com.test.getString(int, java.lang.Object))' in Type 'com.test' (AspectTest.java:14) advised by around advice from 'com.test.AuditLoggerAspect' (AuditLoggerAspect.class(from AuditLoggerAspect.java))
This app packaging is WAR and i want to deploy it on Weblogic.
When I deploy it or run test the aspect not work. Why?
I test this aspect and maven plugin config in Java App and it's work good.
When I clean and package the project, the maven-compiler-plugin recompiles the projects after aspectj-compiler-plugin compiled it. So the compiled aspects are removed.
I added the below config to maven-compiler-plugin to solve it:
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<!-- IMPORTANT -->
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>

How to disable AspectJ compilation via configuration

I implement my aspect class in Java style:
#Aspect
public class SomeAspect {
#Pointcut("...")
public void somePointcut() {}
#Around("somePointcut()")
public ...
}
I am new to AspectJ. I want to know if there a convenient/centralized way to control the effectiveness of the Pointcut so I can enable/disable them based on some configuration at compile time.
During runtime you can just use an if() pointcut, but I know this is not what you want.
For compile time it depends on how you build your project. I am going to assume you use Maven and the AspectJ Maven Plugin.
Let us further assume that you configure the plugin to run in the process-sources phase like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.8</version>
<configuration>
<!--<showWeaveInfo>true</showWeaveInfo>-->
<source>${java.source-target.version}</source>
<target>${java.source-target.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.source-target.version}</complianceLevel>
<encoding>${project.build.sourceEncoding}</encoding>
<!--<verbose>true</verbose>-->
<!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
Then just add a Maven profile - let us call it skip-aspectj which overwrites the phase to none via <phase/>:
<profiles>
<profile>
<id>skip-aspectj</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<phase/>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Now if you run your Maven build
normally, the aspects are compiled and applied,
with mvn -P skip-aspectj ... they will be inactive and the whole AspectJ Maven execution just skipped.

AspectJ Maven Plugin cannot compile my project

I try to use aspectj maven plugin for compile project with aspectj compiler and then I try to package classes into "war" file. Unfortunately, it doesn't work with following configuration (pom.xml):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>com.liferay.maven.plugins</groupId>
<artifactId>liferay-maven-plugin</artifactId>
<version>${liferay.maven.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<autoDeployDir>${liferay.auto.deploy.dir}</autoDeployDir>
<appServerDeployDir>${liferay.app.server.deploy.dir}</appServerDeployDir>
<appServerLibGlobalDir>${liferay.app.server.lib.global.dir}</appServerLibGlobalDir>
<appServerPortalDir>${liferay.app.server.portal.dir}</appServerPortalDir>
<liferayVersion>${liferay.version}</liferayVersion>
<pluginType>portlet</pluginType>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
<showWarnings>true</showWarnings>
<failOnError>true</failOnError>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilationLevel>1.7</compilationLevel>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
<type>jar</type>
</dependency>
After mvn clean install I see following exceptions:
[INFO] --- aspectj-maven-plugin:1.7:compile (default) # tvbs-portlet ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[ERROR] Missing message: configure.incompatibleComplianceForSource in: org.aspectj.ajdt.ajc.messages
<unknown source file>:<no line information>
[ERROR] no sources specified
<unknown source file>:<no line information>
[ERROR] AspectJ Compiler 1.8.2
Usage: <options> <source file | #argfile>..
AspectJ-specific options:
-inpath <list> use classes in dirs and jars/zips in <list> as source
Could anybody suggest me some solution?
It seems like a known issue http://jira.codehaus.org/browse/MASPECTJ-125
You can fix it by adding the following to your pom file.
<complianceLevel>1.6</complianceLevel>
Update: While the things I said about AspectJ Maven configuration in this answer are all correct, the root cause of the concrete problem at hand - bad Maven dependency management - is described in my other answer. It would be better if that one was the accepted answer and not this one.
User codelion's hint makes sense, please change your <compilationLevel> tag (typo?) - to <complianceLevel>.
There is no need to downgrade to plugin version 1.6, you can keep 1.7.
There is also no need to specify the configuration again within the <execution> section, the one at plugin level is enough.
Please note that the default AspectJ version in plugin 1.7 is 1.8.2, so maybe your runtime dependency on 1.7.4 works, but if I were you I would upgrade that one too, optimally in sync with the plugin version. It is no hard requirement, but I think it makes sense.
Maybe you even want to upgrade to the current version AspectJ 1.8.4, in the plugin as well as the runtime. This can also be achieved by adding a dependency to the desired aspectjtools version to the plugin configuration:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.source-target.version>1.8</java.source-target.version>
<aspectj.version>1.8.4</aspectj.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>${java.source-target.version}</source>
<target>${java.source-target.version}</target>
<Xlint>ignore</Xlint>
<complianceLevel>${java.source-target.version}</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<!-- IMPORTANT -->
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
Having looked at your Maven project https://github.com/dmitrievanthony/test-aspectj I found out that
the problem is totally unrelated to AspectJ Maven Plugin,
the same compilation errors also occur in Maven Compiler Plugin and
that the root cause of your problem is simply bad dependency management.
Here is a screenshot (full size here) from IntelliJ IDEA's "find class":
As you can see, class LockModeType is found in 3 (three!) dependencies, one of which contains a version of the class which does not contain the expected enum values. Your code compiles if you remove this dependency.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>ejb3-persistence</artifactId>
<version>1.0.2.GA</version>
</dependency>
Maybe you should clean up your dependencies. You can use the Maven Dependency Plugin with goals like dependency:analyze and dependency:tree for that purpose.
It will be work after change plugin configuration to following:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<complianceLevel>1.7</complianceLevel>
<source>1.7</source>
<target>1.7</target>
</configuration>
</execution>
</executions>
</plugin>
But after this I get a lot of different compilation errors:
[ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.6:compile (default) on project tvbs-portlet: Compiler errors:
[ERROR] error at Entitle.class, entitleId, LockModeType.PESSIMISTIC_WRITE);
[ERROR]
[ERROR] /Users/<...>/ejb/BillingEJB.java:43:0::0 PESSIMISTIC_WRITE cannot be resolved or is not a field
[ERROR] error at .createQuery("select e from Entitle e " +
[ERROR]
[ERROR] /Users/<...>/ejb/EntitleEJB.java:62:0::0 The method createQuery(String) in the type EntityManager is not applicable for the arguments (String, Class<Entitle>)
[ERROR] error at return entityManager.createQuery(
[ERROR] ^^
Can cause is incorrect aspectj plugin parameters?
make sure the modules has source code,like *.java etc.
when i compile CAS on version 4.0.6 it happens this error, I found the cas-server-uber-webapp doesn't has any source code in src folder. just remove the module from parent pom.xml.

Categories