Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you tell Maven which Tests to run?

Tags:

java

spring

maven

I am following this tutorial and have reached the testing portion. When I create the HelloControllerTest file as well as a HelloControllerIT file in my test directory at src/test/java/hello/ only the HelloControllerTest test runs. However, if I rename the second file to end with the word Test, such as HelloController2Test, then it also runs. I'm running the tests from command line during the Maven build with mvn clean verify. I'm running Maven 3.3.9 on OSX.

I have two main questions: how does Maven know which tests to run? And, more importantly, how do I tell Maven to run other tests? Below is my pom.xml, taken directly from the tutorial:

<?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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
like image 889
Teofrostus Avatar asked Jan 07 '23 09:01

Teofrostus


2 Answers

The maven-surefire-plugin will, by default, attempt to execute all tests that follow the pattern of Test*.java or *Test.java or *TestCase.java. It ignores your HelloControllerIT very intentionally, as by standard maven convention, that is not a unit test, it is an integration test. The maven-surefire-plugin is enabled by default in all maven projects.

There is a separate plugin, maven-failsafe-plugin, used to run integration tests, denoted (by default) by the naming pattern IT*.java or *IT.java or *ITCase.java. It runs during the integration-test phase of the build rather than the test phase. However, unlike maven-surefire-plugin, you need to explicitly enable it. (I don't know why this is.)

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>
          <goal>verify</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Add maven-failsafe-plugin to your project, and your integration test should run just fine.

like image 134
dcsohl Avatar answered Jan 17 '23 13:01

dcsohl


1) how does Maven know which tests to run? By default, maven executes all tests. I mean, all test that Maven finds.

2) how do I tell Maven to run other tests? To execute a specific test class (eg. HelloController2Test), you can do this :

mvn -Dtest=HelloController2Test test

Reference doc here

One other explanation(from the well known site MKYong)

like image 25
jpmottin Avatar answered Jan 17 '23 13:01

jpmottin