I have a project Test-A, in this I have junit test class for the application.
Now, I have another project called Test-B, in this I have junit test class for the application.
Test-B, I have included project-A i.e modified POM.xml and included as:
POM.xml:
<dependency>
<groupId>com.abc.test-A</groupId>
<artifactId>TestA</artifactId>
<version>1.7</version>
<scope>test</scope>
</dependency>
question:
When I run mvn test for Test-B project, this has to run Test-A junit test files also. how can I run this kind of tests?
I am junit 4.9 version.
You will have to use the Maven Source Plugin to package the test source.
Here is an example for the build plugin to package the test source when you install or deploy the project:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>verify</phase>
<goals>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
You will have to add the dependency for the tests as a test-jar type:
<dependency>
<groupId>com.abc.test-A</groupId>
<artifactId>TestA</artifactId>
<version>1.7</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Now you have access to the tests from artifact TestA that you can run directly.
Running tests for your project B will not run tests for any dependency. For example your project would also have a dependency to junit. You don't expect those tests to run.
Related
I have a custom Jenkins plugin that I've built. I have some test cases that leverage some code out of the jenkins-test-harness project (namely the Junit JenkinsRule.)
Anyway, the unit tests run and pass when I run them in IntelliJ. No exceptions, no errors.
When I try to run them from the Maven commandline, however, all of the tests that rely on the JenkinsRule fail:
[ERROR] testGetLastDate(com.mycompany.myplugin.portlet.utils.UtilsHudsonTest) Time elapsed: 0 s <<< ERROR!
java.lang.NoClassDefFoundError: Could not initialize class org.jvnet.hudson.test.TestPluginManager
at org.jvnet.hudson.test.JenkinsRule.<init>(JenkinsRule.java:325)
at com.mycompany.myplugin.portlet.utils.UtilsHudsonTest.<init>(UtilsHudsonTest.java:19)
That TestPluginManager is in the jenkins-test-harness jar. The JenkinsRule that attempts to call it is right next to it in the same package in the same jar, so clearly the jar itself is successfully on the classpath.
I can't quite figure out why I can't run this job from the commandline using mvn test.
My POM is very simple -- I declare a Parent on the Jenkins plugin-3.2 pom:
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.2</version>
<relativePath/>
</parent>
And I have a dependency on the test harness:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>dashboard-view</artifactId>
<version>${dashboard-view.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-test-harness</artifactId>
<scope>test</scope>
<version>2.34</version>
</dependency>
</dependencies>
(That's the entire dependencies section, no omissions.)
And in my build section, I only declare the compiler, surefire, and hpi plugins:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>hpi</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</plugins>
</build>
(The surefire config is inherited from the plugin-3.2 parent.)
Here's the bit of the UtilHudsonTest that causes the error -- line 19 is the #Rule annotation:
public class UtilsHudsonTest {
#Rule // This is line 19
public JenkinsRule j = new JenkinsRule();
#Test
public void testGetLastDate() throws Exception {
FreeStyleProject prj = j.createFreeStyleProject("prj1");
prj.scheduleBuild2(0).get();
FreeStyleProject prj2 = j.createFreeStyleProject("prj2");
prj2.scheduleBuild2(0).get();
List<Job> jobs = new ArrayList<>();
jobs.add(prj);
jobs.add(prj2);
LocalDate lastDate = Utils.getLastDate(jobs);
assertNotNull(lastDate);
}
// other tests
}
I'm kind of stumped why this would work in IntelliJ and not in Maven. A mvn dependency:tree shows that the correct and expected dependencies. Any ideas in how to get this plugin to successfully run its test from commandline?
This is the scenario, I have a group of a AMPs, some developed by myself, and other developed by other developer/vendors.
If I am not wrong, using the Maven SDK I can develop and run only one specific AMP at a time.
What steps can be taken to have an external AMP being deployed along with the main project AMP at start up which is when running mvn integration-test -Pamp-to-war.
In particular I am interested in having Alfresco load the wcmqs module.
Assuming you already have the external amps available to maven (either because their're on Maven Central repo or because they're installed locally), you simply add the external amps as dependencies in your amp project. E.g.:
<dependency>
<groupId>org.sharextras</groupId>
<artifactId>javascript-console-repo</artifactId>
<version>0.6.0</version>
<type>amp</type>
</dependency>
You also must configure the maven dependency plugin. You can do it in a profile so it can be turned on or off depending on your needs:
<profiles>
<profile>
<id>unpack-deps</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-amps</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeTypes>amp</includeTypes>
<outputDirectory>${alfresco.client.war.folder}</outputDirectory>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.alfresco.maven.plugin</groupId>
<artifactId>maven-amp-plugin</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This way, you can start the main project amp plus its dependencies with the following command:
mvn integration-test -Pamp-to-war -Punpack-deps
For a complete pom.xml example see: https://github.com/douglascrp/alfresco-value-assistance/blob/master/alfresco-value-assistance-repo/pom.xml
I have a multi module maven project with a structure
parent
pom.xml
module
pom.xml
core-api
pom.xml
integ-tests
pom.xml
I have maven surefire plugin setup for executing the unit tests '*Test.java' which are houses in the 'core-api' module.
We have slow long-running integration tests housed in a separate 'integ-tests' module. we use '*Test.java' for our integ tests as well.
We need to be able to compile all source code but want to exclude the 'integ-test' from running as part of the default maven 'test' phase. We plan to use a profile to enable the test phase of the 'integ-test' module. I don't want to use the 'failsafe' plugin.
A matrix outlining the combination is here
mvn | core | integ-test
test | run unit tests | exclude
test -PintegTest | unit tests | integ tests
I've defined the surefire plugin in my parent pom, with a property 'skip.integ.tests' which will be controlled via a profile '-PintegTests'.
<properties>
<skip.integ.tests>true</skip.integ.tests>
</properties>
..
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
</build>
..
<profiles>
<profile>
<id>integTests</id>
<properties>
<skip.integ.tests>false</skip.integ.tests>
</properties>
</profile>
</profiles>
In my 'integ-test' pom, i've then overridden the 'maven-surefire-plugin' config and have the 'skipTests' configuration set to look at the value of the property.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${skip.integ.tests}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
My problem is the integ-test module tests run in every case. Any ideas on where i'm going wrong with the setup?
First you should name your integration tests accordingly to the naming conventions of the maven-failsafe-plugin which is intended to run the integration tests. Furthermore the pre-integration-test, integration-test and post-integration-test life cycle phases are intended for running those tests. This means in your case to configure the maven-failsafe-plugin accordingly to the documentation like this. The maven-failsafe-plugin bounds to the integration-test life cycle phase.
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
I would suggest to add the following profile into your integration test module like this:
<profiles>
<profile>
<id>run-its</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This gives you simply the following options:
mvn test
running unit tests only
mvn -DskipTests=true test
running compilation etc. but no unit tests.
mvn -Prun-its verify
Running packaging etc. unit tests and integration tests
mvn install
Running installation without integration tests.
mvn -DskipTests=true install
Running installation without running unit tests nor integration tests.
In your Maven build, you can exclude:
compilation and execution of both unit tests (by Surefire plugin) and integration tests (by Failsafe plugin) by adding -Dmaven.skip.test=true
execution of both unit and integration tests via -DskipTests
execution of integration tests via -DskipITs
Instead, if you have your integration tests in a separate Maven module (i.e. integ-test in your case), you can directly exclude that from the Maven build via profile, like in below example -- see extract of aggregator's pom.xml and maven command line to be launched:
<modules>
<!-- remove 'integ-test' from this list -->
</modules>
<profiles>
<profile><id>build-it</id>
<activation><activeByDefault>true</activeByDefault></activation>
<modules><module>integ-test</module></modules>
</profile>
</profiles>
and then mvn install -P !build-it
Let's suppose that I have a bunch of code that I need across a lot of projects, but just in tests.
So, I want to create a separated maven project for it, for example, carlos-test-utils, and ad it as a test dependency in my projects.
But, in my carlos-test-utils project, I also need JUnit. So, I add it as a test dependency, which obviously doesn't work, because I put my code in src/main/java.
I would like to hear which is the best way to deal with this kind of thing.
Put deps as provided?
Some dirty tricky thing to copy .java files across the projects?
Any other thing?
You can manage that via the maven-jar-plugin which should be used for such cases in the following way:
This should be done in the module/project which should provide the test classes and
the code should be put in the usual folder for test code src/test/java and not in src/main/java. All the dependencies your Test code needs should be added as usual dependencies with scope test.
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
...
</project>
In the project you like to use the test dependencies just use it like this:
<project>
...
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<type>test-jar</type>
<version>version</version>
<scope>test</scope>
</dependency>
</dependencies>
...
</project>
I'm using Maven 3.0.3.
Is it possible to include a dependency for my test phase only, and then another dependency for my integration-phase only? When these two dependencies are included together
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<version>${gwtVersion}</version>
<scope>test</scope>
</dependency>
...
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>
I get a java.lang.NoSuchMethodError: org.apache.http.conn.scheme.Scheme.<init> error when running my Selenium integration tests. When the GWT dependency is excluded, the Selenium tests run. I still need the GWT dependency for the test phase, tho.
With respect to the answers given, the one I liked best was simply adding a "classpathDependencyExcludes" to my failsafe-plugin execution ...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/integration/**</include>
</includes>
<systemPropertyVariables>
<tomcat.port>${tomcat.servlet.port}</tomcat.port>
<project.artifactId>${project.artifactId}</project.artifactId>
</systemPropertyVariables>
<classpathDependencyExcludes>
<classpathDependencyExcludes>com.google.gwt:gwt-dev</classpathDependencyExcludes>
</classpathDependencyExcludes>
</configuration>
</execution>
</executions>
</plugin>
That ensured that the problematic dependency (in this case gwt-dev), would not appear when running the integration-test phase.
Use profiles. A profile allows you to add dependencies depending on the arguments of the -P command line option.
Different dependency sets in Maven profiles are the only way to achieve this, since the "test" scope encloses both "test" and "integration-test" phase.
I would suggest to have separate project(s) with test cases