Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Maven different unit Test from integration Test?

This is my project structure in mvn:

enter image description here

As you can notice, I have two classes in src/test/java

  1. CardValidtorIT.java (THis is integration test)

  2. CardValidatorTest.java (This is Unit-Test)

when I run

mvn package

I notice only the unit-test (CardValidatorTest.java) is run

But when I run

mvn integration-test

I see both unit-test and Integration tests are run.

How does mvn know not to execute the CardValidatorIT.java when I run mvn package. That is, why it did not run CardValidatoryIT.java

Here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>chapter14</artifactId>
    <groupId>org.agoncal.book.javaee7</groupId>
    <version>1.0</version>
</parent>
<groupId>org.agoncal.book.javaee7.chapter14</groupId> <artifactId>chapter14-service</artifactId> <version>1.0</version>
<packaging>war</packaging>
  <dependencies>
    <dependency>
<groupId>org.glassfish.main.extras</groupId> <artifactId>glassfish-embedded-all</artifactId> <version>4.0</version>
<scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
<groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.12.4</version>
<executions>
          <execution>
            <id>integration-test</id>
            <goals>
<goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

P.S: I do know that I have a integration-test goal in mvn. but I have not tied the goal to which class should be run during integration test

Thanks

like image 748
brain storm Avatar asked Sep 26 '14 19:09

brain storm


People also ask

How is unit testing and integration testing different?

While unit tests always take results from a single unit, such as a function call, integration tests may aggregate results from various parts and sources. In an integration test, there is no need to mock away parts of the application. You can replace external systems, but the application works in an integrated way.

How does Maven run integration tests?

The simplest way to run integration tests is to use the Maven failsafe plugin. By default, the Maven surefire plugin executes unit tests during the test phase, while the failsafe plugin runs integration tests in the integration-test phase.

What's the difference between unit functional acceptance and integration tests?

Integration testing focuses on ensuring various components within a program or system can function together well. Acceptance testing focuses on the client's use of the system and how it functions as a whole unit, rather than on the specific interaction between different aspects.


3 Answers

See Maven Surefire.

This plugin is responsible for mvn test in Maven. The default configuration is coming into play. This means the class with the word Test will come into play when you run mvn test and in your case mvn package

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

When you run mvn integration-test the failsafe plugin is used. It's default inclusion/exclusion rules differ - by default it looks for the word IT, for example.

http://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html

Note: It is odd to me that the test class CardValidatorTest is picked up when you run mvn integration-test. Based, on how I read the default inclusion and exclusion rules for the failsafe-plugin, I would not expect this. In fact, when I adapt your pom.xml to my own sample project, I don't see that behavior. Instead all Test classes are picked up with mvn test and mvn package. All IT classes are picked up with mvn integration-test. Are you sure you don't have some code level dependency on the two classes? Other than changed inclusion/exclusion rule, that's the only thing that I can think of that might cause them both to be picked up with mvn test or mvn package.

like image 58
Doug Simmons Avatar answered Oct 23 '22 23:10

Doug Simmons


The Failsafe plugin in Maven handles integration testing. By default, these are the include patterns for integration tests:

"**/IT*.java" - includes all of its subdirectories and all java filenames that start with "IT".
"**/*IT.java" - includes all of its subdirectories and all java filenames that end with "IT".
"**/*ITCase.java" - includes all of its subdirectories and all java filenames that end with "ITCase".

Failsafe is not part of Maven's default lifecycle bindings, so integration tests like CardValidatorIT, which of course satisfies the default patterns, do not run as part of the lifecycle. This gets to Maven's opinionated conventions for which failures should fail the build and which tests should run all the time (fast unit tests with wide code coverage) vs. which tests should run less often (slow integration tests).

Of course, you can override conventions as you see fit.

like image 27
Vidya Avatar answered Oct 23 '22 23:10

Vidya


The integration-test build phase comes after the package build phase. So "mvn package" will not reach the "integration-test" build phase.

like image 38
user6378593 Avatar answered Oct 23 '22 23:10

user6378593