11
by Holasz Kati & Ilies Amelia MAVEN SureFire vs. FailSafe

MAVEN - Short documentation

Embed Size (px)

Citation preview

by Holasz Kati & Ilies Amelia

MAVEN

SureFire vs. FailSafe

by Holasz Kati & Ilies Amelia

Contents

Running tests in parallel ................................................................................................................. 3

Skipping Tests ................................................................................................................................ 3

Inclusions and Exclusions of Tests ............................................................................................... 4

Inclusions .................................................................................................................................... 4

Exclusions.................................................................................................................................... 4

Running a Single Test.................................................................................................................... 5

Running a set of methods in a Single Test Class .......................................................................... 5

Install Plugin .................................................................................................................................. 6

The install:install-file goal .......................................................................................................... 6

QUESTIONS… .............................................................................................................................. 7

How To Include Custom Library Into Maven Local Repository? ............................................... 7

How To Add Remote Repository In Maven? ............................................................................... 7

How To Display Maven Plugin Goals And Parameters? ........................................................... 8

Useful Explanations ....................................................................................................................... 9

Unit Testing ................................................................................................................................. 9

Integration Testing ...................................................................................................................... 9

Mojo ............................................................................................................................................ 9

Plug-in ......................................................................................................................................... 9

Build Lifecycle ........................................................................................................................... 10

References .................................................................................................................................... 11

by Holasz Kati & Ilies Amelia

Running tests in parallel

To do this, you must set the parallel parameter, and may change the threadCount or

useUnlimitedThreads attribute. For example:

<plugins>

[...]

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.16</version>

<configuration>

<parallel>methods</parallel>

<threadCount>10</threadCount>

</configuration>

</plugin>

[...]

</plugins>

Skipping Tests

To skip running the tests for a particular project, set the skipTests property to true.

<project>

[...]

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.16</version>

<configuration>

<skipTests>true</skipTests>

</configuration>

</plugin>

</plugins>

</build>

[...]

by Holasz Kati & Ilies Amelia

</project>

You can also skip the tests via command line by executing the following command:

mvn install -DskipTests

Inclusions and Exclusions of Tests

Inclusions

By default, the Surefire Plugin will automatically include all test classes with the following wildcard

patterns: "**/Test*.java" , “**/*Test.java", "**/*TestCase.java".

<project>

[...]

<build>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.16</version>

<configuration>

<includes>

<include>Sample.java</include>

</includes>

</configuration>

</plugin>

</plugins>

</build>

[...]

</project>

Exclusions

Exclusions can be done by configuring the excludes property of the plugin.

<project>

[...]

<build>

<plugins>

by Holasz Kati & Ilies Amelia

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.16</version>

<configuration>

<excludes>

<exclude>**/TestCircle.java</exclude>

<exclude>**/TestSquare.java</exclude>

</excludes>

</configuration>

</plugin>

</plugins>

</build>

[...]

</project>

Running a Single Test

To run this through Maven, set the test property to a specific test case.

mvn -Dtest=TestCircle test

You may also use patterns to run a number of tests: mvn -Dtest=TestCi*le test

And you may use multiple names/patterns, separated by commas:

mvn -Dtest=TestSquare,TestCi*le test

Running a set of methods in a Single Test Class

You must use the following syntax

mvn -Dtest=TestCircle#mytest test

You can use patterns too

mvn -Dtest=TestCircle#test* test

As of surefire 2.12.1, you can select multiple methods:

by Holasz Kati & Ilies Amelia

mvn -Dtest=TestCircle#testOne+testTwo test

Install Plugin

With the maven-install-plugin you can put your artifacts in the local repository.

The install:install-file goal

The install:install-file goal is used primarily for installing artifacts to the local repository

which were not built by Maven. The project's development team may or may not

provide a POM for the artifact. Here's a list of some of the available parameters for

the install-file goal:

mvn install:install-file -Dfile=your-artifact-1.0.jar \

[-DpomFile=your-pom.xml] \

[-Dsources=src.jar] \

[-Djavadoc=apidocs.jar] \

[-DgroupId=org.some.group] \

[-DartifactId=your-artifact] \

[-Dversion=1.0] \

[-Dpackaging=jar] \

[-Dclassifier=sources] \

[-DgeneratePom=true] \

[-DcreateChecksum=true]

the groupId, artifactId, version and packaging of the file to install. These can be taken

from the specified pomFile, extracted from the pom.xml inside the artifact, and

overridden or specified using the command line. When the pomFile contains

a parent section, the parent's groupId can be considered if the groupId is not specified

further for the current project or on the command line.

Ex: mvn -X install:install-file

-Dfile=path/to/mly/jar

-DgroupId=j<groupname>

-DartifactId=<artifactname>

-Dversion=<version>

-Dpackaging=jar

by Holasz Kati & Ilies Amelia

QUESTIONS…

How To Include Custom Library Into Maven Local Repository?

For example, kaptcha, a popular third party Java library, which is used to generate “captcha”

image to stop spam, but it’s not available in the Maven center repository.

In this tutorial, we will show you how to install the “kaptcha” jar into your Maven’s local

repository.

mvn install

Download the “kaptcha“, extract it and copy the kapcha-version.jar to somewhere else, for

example, c drive. Issue following command:

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar

-DgroupId=com.google.code

-DartifactId=kaptcha

-Dversion={version}

-Dpackaging=jar

pom.xml

o declare the kaptcha coordinate in pom.xml.

<dependency>

<groupId>com.google.code</groupId>

<artifactId>kaptcha</artifactId>

<version>2.3</version>

</dependency>

Done

Build it, now the “kaptcha” jar is able to retrieve from your Maven local repository.

How To Add Remote Repository In Maven?

By default, Maven download all dependencies from Maven Central Repository. But, some

libraries are missing in central repository, and only available in remote repository.

by Holasz Kati & Ilies Amelia

Ex: Java.net Repository

Add Java.net remote repository details in pom.xml file.

<project ...>

<repositories>

<repository>

<id>java.net</id>

<url>https://maven.java.net/content/repositories/public/</url>

</repository>

</repositories>

</project>

How To Display Maven Plugin Goals And Parameters?

mvn help:describe -Dplugin=eclipse

List all of the available goals of maven-eclipse plugin.

mvn help:describe -Dplugin=eclipse

mvn help:describe -Dplugin=eclipse -Dmojo=eclipse

Display the detail of “eclipse” parameter of maven-eclipse plugin, with full explanation.

mvn help:describe -Dplugin=eclipse -Dmojo=eclipse -Dfull=true

Note : “mojo” is means the parameter of plugin.

by Holasz Kati & Ilies Amelia

Useful Explanations

Unit Testing The primary goal of unit testing is to take the smallest piece of testable software in the

application, isolate it from the remainder of the code, and determine whether it behaves

exactly as you expect. Each unit is tested separately before integrating them into modules

to test the interfaces between modules.

Integration Testing

Integration testing is a logical extension of unit testing. In its simplest form, two units

that have already been tested are combined into a component and the interface between

them is tested.

The idea is to test combinations of pieces and eventually expand the process to test your

modules with those of other groups. Eventually all the modules making up a process are

tested together. Beyond that, if the program is composed of more than one process, they

should be tested in pairs rather than all at once.

Integration testing identifies problems that occur when units are combined. By using a

test plan that requires you to test each unit and ensure the viability of each before

combining units, you know that any errors discovered when combining units are likely

related to the interface between units. This method reduces the number of possibilities to

a far simpler level of analysis.

It occurs after unit testing and before validation testing.

Mojo

A Mojo is really just a goal in Maven 2, and plug-ins consist of any number of goals

(Mojos). Mojos can be defined as annotated Java classes or Beanshell script. A Mojo

specifies metadata about a goal: a goal name, which phase of the lifecycle it fits into, and

the parameters it is expecting.

MOJO is a play on POJO (Plain-old-Java-object), substituting "Maven" for "Plain".

Plug-in "Maven" is really just a core framework for a collection of Maven Plugins. In other

words, plugins are where much of the real action is performed, plugins are used to: create

jar files, create war files, compile code, unit test code, create project documentation, and

on and on. Almost any action that you can think of performing on a project is

implemented as a Maven plugin.

Plugins are the central feature of Maven that allow for the reuse of common build logic

across multiple projects. They do this by executing an "action" (i.e. creating a WAR file

or compiling unit tests) in the context of a project's description - the Project Object Model

by Holasz Kati & Ilies Amelia

(POM). Plugin behavior can be customized through a set of unique parameters which are

exposed by a description of each plugin goal (or Mojo).

Build Lifecycle is a series of common stages through which all project builds naturally progress. Plugin

goals are bound to specific stages in the lifecycle.

by Holasz Kati & Ilies Amelia

References

http://maven.apache.org

http://maven.apache.org/plugins/index.html

SureFire Plug-in details: http://maven.apache.org/surefire/

FailSafe Plug-in details: http://maven.apache.org/surefire/maven-failsafe-plugin/

http://en.wikipedia.org/wiki/Integration_testing

http://en.wikipedia.org/wiki/Unit_testing

http://en.wikipedia.org/wiki/Apache_Maven

http://maven.apache.org/guides/introduction/introduction-to-plugins.html

http://maven.apache.org/plugins/index.html