I have the same problem as Junit4 and TestNG in one project with Maven but I still have issues.
I created simple project where I need to use junit4 and testng
Here is root pom:
<groupId>com.dimas.testmodule</groupId>
<artifactId>TheParent</artifactId>
<version>0.0.001</version>
<name>TheParent</name>
<packaging>pom</packaging>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
<spring.scope>test</spring.scope>
<maven.surefire.plugin.version>2.9</maven.surefire.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
...etc
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<modules>
<module>JUnitOnly</module>
<module>TestNGOnly</module>
<module>testmodule</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<excludes>
<exclude>**/*NGTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>testNGonly</id>
<modules>
<module>JUnitOnly</module>
<module>TestNGOnly</module>
<module>testmodule</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<includes>
<include>**/*NGTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
modules JUnitOnly and TestModule just inherits and do not overrite any properties. And here are TestNgOnly
<groupId>com.dimas.testmodule.testngonly</groupId>
<artifactId>TestNGOnly</artifactId>
<version>0.0.001</version>
<name>testngonly</name>
<parent>
<groupId>com.dimas.testmodule</groupId>
<artifactId>TheParent</artifactId>
<version>0.0.001</version>
</parent>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>testNGtest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
<suiteXmlFiles>
<suiteXmlFile>
<!--test/resources/my-testng.xml-->
src\test\resources\my-testng.xml
</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Well I need to ignore testng tests for default profile and launch only testng tests for profile testNGOnly.
There are three modules - junit, testng and mixed.
The default profile should execute only junit tests - i.e. run junit module and run junit tests of mixed module. testNG profile should execute only testNG tests - i.e. run testNG module and run testNG tests of mixed module.
The way profiles work in maven, it is not possible to exclude a module in a profile. However, there is no need to build a module which is not required.
Thus, in default profile, have modules junit and mixed.
In testNG profile, add module testNG.
Following maven best practice, define <dependencies> in the parent pom within <dependencyManagement>. This allows us to only use junit dependency in junit module and testNG dependency in testNG module.
There is nothing common about surefire configuration across the three modules and as such not required to be specified in parent pom.
Since it not required to run junit tests in testNG profile, add a plugin configuration to skipTests in testNG profile for junit module.
Configure the mixed pom using the tip in this answer for the related SO question. This gets tricky since surefire needs to use junit or testng runner based on the profile.
I have incorporated the above suggestions in a sample project here.
Related
I'm using IntelliJ with Maven for my project and i have to analyze it with SonarQube, but when I scan my project with the command provided by the webApp it only analyzes the pom.xml file ignoring the rest of the project.
Analysis results
clean install sonar:sonar -Dsonar.host.url=http://localhost:9000 -Dsonar.login="the corresponding key here"
My pom.xml (what is inside the project tag):
<groupId>cl.itau.simulacionCredito</groupId>
<artifactId>SimulacionCreditoIntelliJ</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.72</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<excludes>
<exclude>**/HomeTest.java</exclude>
<exclude>**/PropuestaTest.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.5.0.1254</version>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>SimulacionCredito</id>
<name>SimulacionCredito</name>
<url>file:C:\Users\Pc\IdeaProjects</url>
</repository>
</distributionManagement>
I also tried to use this inside the configuration tag:
<sonar.sources>src/main/java</sonar.sources>
And this happened:
Second analysis
My settings.xml in Maven's conf:
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
</mirrors>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
I've been adding things to the POM because of different errors while trying to fix this.
I have the latest version of SonarQube installed.
Sonarqube works by analyzing jacoco outputs. I don't see any configs for jacoco in your pom. https://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/ is a good tutorial on how to set it up.
You will also have to tell sonarqube where your jacoco.exec file is at.
https://docs.sonarqube.org/display/PLUG/Code+Coverage+by+Unit+Tests+for+Java+Project
So, the problem was that IntelliJ was configured with Java SE Development Kit 11, I downgraded to Java SE Development Kit 8u181 and configured the POM so Maven works with that change, I executed the scanner again and it worked.
Changes to pom.xml:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<sonar.sources>src/main/java</sonar.sources>
</properties>
added:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
New analysis results
Can anyone tell me how to execute single testcase with maven or jenkins.
mvn -Dtest=smoke tests seem to be not working for me.
I have tried other options like mvn clean install -Dtest=smoketest test that time i got below error if is set to true, and no tests execute if set to false`
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project ABC: No tests were executed! (Set -DfailIfNoTests=false to ignore this error.)
POM USED
<project>
<groupId>com.testng.smoke</groupId>
<artifactId>ABC</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build> <!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>
<plugins> <!-- plugins executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<systemPropertyVariables>
<AppName>XYX</AppName>
<browserName>chrome</browserName>
<isLocal>true</isLocal>
</systemPropertyVariables>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>${suiteFile}</suiteXmlFile>
</suiteXmlFiles>
<groups>${chooseGroup}</groups>
<includes>
<include>**.java</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
<argLine>
-javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Compiler plugin configures the java version to be usedfor compiling the code -->
</plugins>
</build>
</project>
It doesnt matter if its jenkins or local build.
You have to specify you test in the following way:
mvn -Dtest=TestClass#testmethod test
You forgot about that #.
I have a project that includes multiple other jar artifacts as dependencies. I'm using the surefire plugin's dependenciesToScan property to run tests in the said artifacts as follows:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<dependenciesToScan>
<dependency>com.example.tests:project-a</dependency>
<dependency>com.example.tests:project-b</dependency>
</dependenciesToScan>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.example.tests</groupId>
<artifactId>project-a</artifactId>
</dependency>
<dependency>
<groupId>com.example.tests</groupId>
<artifactId>project-b</artifactId>
</dependency>
</dependencies>
Ideally, I would like to be able to do the following:
mvn test would run the tests in each dependency like normal
Another parameter would run only the tests for whichever artifact is specified and skip the tests for whatever dependency was not included
Is this possible at all or is there another way to approach this?
You can use profiles to have two distinct builds.
<profiles>
<profile>
<id>project-a</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<dependenciesToScan>
<dependency>com.example.tests:project-a</dependency>
</dependenciesToScan>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>project-b</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<dependenciesToScan>
<dependency>com.example.tests:project-b</dependency>
</dependenciesToScan>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The use mvn clean test -P project-a or mvn clean test -P project-b
You could also set different properties in each profiles and have a centralized surefire config.
Or you could use a property:
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<dependenciesToScan>
<dependency>${someProperty}</dependency>
</dependenciesToScan>
</configuration>
</plugin>
</plugins>
</build>
The use mvn clean test -DsomeProperty=project-a
I have 3 projects:
myLibProject: has 2 profiles, "profileOne" and "profileTwo": each profile compile a different jar.
myFirstProject: has the jar compiled by myLibProject with "profileOne" as dependecy
mySecondProject: has the jar compiled by myLibProject with "profileTwo" as dependecy.
It's possible to add myLibProject with a custom profile as depedency?
myLibProject fragment profile: (it's just a sample)
<profiles>
<profile>
<id>profileOne</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*prop*</exclude>
</excludes>
<finalName>jarFromProfileOne-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>compimpl</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.sql</exclude>
</excludes>
<finalName>jarFromProfileTwo-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
myFirstProject has as depency
<dependency>
<groupId>com.mywebsite</groupId>
<artifactId>myLibProject</artifactId>
<version>1.0.0</version>
<type>pom</type>
<with-profile>profileOne</with-profile>
</dependency>
mySecondProject has as depency
<dependency>
<groupId>com.mywebsite</groupId>
<artifactId>myLibProject</artifactId>
<version>1.0.0</version>
<type>pom</type>
<with-profile>profileTwo</with-profile>
</dependency>
how can I achieve this result? Of course i know "with-profile" does not exists as option.
I need 2 different jars because Documentum's requirment, I know Maven it's created to make a single jar... so I can't use envirnment vars or scope.
Maven 3.0.1
I am attempting to build a full site for my multi-module project in one command that I can execute in Jenkins. However when I build it, the submodule javadocs appear in the site root (site/apidocs instead of site/submodule/apidocs). All the links from the parent to submodule are broken as well.
Organization is the standard
pom.xml
submodule/pom.xml
Parent POM contains:
<build>
<pluginManagement>
<plugins>
... Various unrelated plugins ...
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>complete-build</id>
<build>
<plugins>
<!--JavaDoc setup for Jars-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<defaultAuthor>Leon Blakey</defaultAuthor>
<defaultVersion>${project.version}</defaultVersion>
<sourcepath>target/delombok</sourcepath>
</configuration>
</plugin>
<!--Deploy site with Mercurial (Hg)-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<dependencies>
<dependency><!-- add support for scm -->
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-scm</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-hg</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
<configuration>
<reportPlugins>
<!--JavaDoc setup for Site-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<defaultAuthor>Leon Blakey</defaultAuthor>
<defaultVersion>${project.version}</defaultVersion>
<sourcepath>target/delombok</sourcepath>
<show>public</show>
</configuration>
</plugin>
... Checkstyle, PMD, Findbugs, etc ...
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
The submodule POM just contains the skeleton plugin definitions for pluginManagement.
When I try to build a site (both on my local machine and Jenkins) I run from the parent
mvn clean install site-deploy -Pcomplete-build
With the above setup, what would cause a submodule to dump its site files in the site root (site/) instead of the submodule directory (site/submodule)? Do I need to use the stanging commands (which I'm avoiding since it breaks the release process)? Is it simply impossible to build a multi-module site in one command?
Use the maven-reactor-plugin to trigger building at the submodule level.