0

I am using junit4 to run test. My test class is as below in test->java->com.example->ProductIT.java When I run mvn clean install on my project, below test is not recognized and not run. However, when I rename the test class to ProductTest, it works. I do not understand the difference. Why does it work when I rename ?

@ContextConfiguration(locations = {"classpath:META-INF/spring/test-config.xml"})
@RunWith(SpringJunit4ClassRunner.class)
public class ProductIT{
   @Autowried
   private ProductServiceImpl serviceImpl;
   
   @Test
   public void test() throws Exception{
      assertNotNull(serviceImpl)
   }

}

pom.xml

<dependencyManagement>
   <dependency>
       <groupId>org.springframework.batch</groupId>
       <artifactId>spring-batch-core></artifactId>
       <version>4.2.4-RELEASE</version>
   </dependency>
   <dependency>
       <groupId>org.springframework.batch</groupId>
       <artifactId>spring-batch-infrastructure></artifactId>
       <version>4.2.4-RELEASE</version>
   </dependency>
</dependencyManagement>

<dependencies>

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-batch></artifactId>
<dependency>

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

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
</build>
Neha
  • 11
  • 2

1 Answers1

0

The *IT postfix (for integration test) is the default test identifier for the Maven Failsafe plugin. Spring Boot projects don't contain this plugin by default and you have to explicitly add it to your project.

The *Test postfix is the default test identifier for the Maven Surefire Plugin which is used as part of the test phase of the default lifecycle: mvn test.

This answer sheds more light on the differences between these plugins.

In case you want to split your tests (unit & integration) and run them separately (first unit, then integration), add the Maven Failsafe Plugin to your project:

<project>
  
  <!-- other dependencies -->
 
  <build>
      <!-- further plugins -->
      <plugin>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M5</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

... and it should recognize your existing ProductIT when you run mvn verify (or mvn install).

For more information on such testing defaults and standards, consider this guide on testing Java applications with Maven.

rieckpil
  • 10,470
  • 3
  • 32
  • 56