Home > Enterprise >  My Pom.xml is only running junit 4 test cases not juint 5 test cases
My Pom.xml is only running junit 4 test cases not juint 5 test cases

Time:09-09

I had unit tests of both junit 4 and junit 5 in my project. Now when i run my test cases through mvn clean install or mvn test. it only runs junit 4 test cases. It does not run junit 5 test cases. Can anyone help me to understand the issue I have in my POM.xml due to which only junit 4 test cases are running with maven commands.

    <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>xyz</groupId>
    <artifactId>xyz</artifactId>
    <version>3.9.34</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <name>tirebatteryservice</name>
    <description>tirebatteryservice</description>

    <properties>
        <tbc.version>1.0.0</tbc.version>
        <jms.version>1.1</jms.version>
        <java.version>1.8</java.version>
        <lombok.version>1.18.4</lombok.version>
        <commons-lang3.version>3.8.1</commons-lang3.version>
        <fastjson.version>2.0.12</fastjson.version>
        <spring.boot.version>2.7.2</spring.boot.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <org.apache.commons.collections.version>3.2.1</org.apache.commons.collections.version>
        <jacoco.maven.plugin.version>0.7.8</jacoco.maven.plugin.version>
        <testng.version>6.8</testng.version>
        <azure.log.version>2.2.1</azure.log.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <elasticsearch.version>6.6.1</elasticsearch.version>
        <ejml.version>0.23</ejml.version>
        <powermock.version>2.0.0-beta.5</powermock.version>
        <log4j2.version>2.16.0</log4j2.version>
        <tbc-common.version>1.8.0-SNAPSHOT</tbc-common.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>io.github.classgraph</groupId>
                    <artifactId>classgraph</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
            <version>4.8.108</version>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>net.gpedro.integrations.slack</groupId>
            <artifactId>slack-webhook</artifactId>
            <version>1.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
            <version>${spring.boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>jms</artifactId>
            <version>${jms.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <!--For Open API V3 Swagger specification-->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.4.4</version>
            <exclusions>
                <exclusion>
                    <groupId>io.github.classgraph</groupId>
                    <artifactId>classgraph</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.collections</artifactId>
            <version>${org.apache.commons.collections.version}</version>
        </dependency>

        <!-- Testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
        </dependency>

        <!-- jacoco -->
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.maven.plugin.version}</version>
        </dependency>

        <!--enables unit tests to run-->
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <classifier>runtime</classifier>
            <scope>test</scope>
            <version>${jacoco.maven.plugin.version}</version>
        </dependency>

        <!-- Application insights -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-spring-boot-starter</artifactId>
            <version>2.5.1</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>applicationinsights-logging-log4j2</artifactId>
            <version>${azure.log.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>${spring.boot.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- search -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <!-- stanford -->
        <dependency>
            <groupId>com.googlecode.efficient-java-matrix-library</groupId>
            <artifactId>ejml</artifactId>
            <version>0.23</version>
        </dependency>
        <!-- sonar -->
        <dependency>
            <groupId>org.codehaus.sonar</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>5.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

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

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.13</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.13</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

<!--        <dependency>-->
<!--            <groupId>org.junit.platform</groupId>-->
<!--            <artifactId>junit-platform-launcher</artifactId>-->
<!--            <version>1.3.2</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->

<!--        <dependency>-->
<!--            <groupId>org.junit.vintage</groupId>-->
<!--            <artifactId>junit-vintage-engine</artifactId>-->
<!--            <version>5.3.2</version>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.sams.member.platform.util</groupId>
            <artifactId>ccpa-access</artifactId>
            <version>0.1.14</version>
        </dependency>

        <dependency>
            <groupId>com.github.kstyrc</groupId>
            <artifactId>embedded-redis</artifactId>
            <version>0.6</version>
        </dependency>

        <!-- Azure Key Vault Dependency  -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-keyvault</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!--TBC COMMON-->
        <dependency>
            <groupId>com.sams.clubops.tbc</groupId>
            <artifactId>tbc-common</artifactId>
            <version>${tbc-common.version}</version>
        </dependency>

        <dependency>
            <groupId>net.sf.barcode4j</groupId>
            <artifactId>barcode4j</artifactId>
            <version>2.1</version>
        </dependency>

        <!--Mockito dependency-->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <fork>true</fork>
                    <mainClass>com.sams.clubops.Application</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.5.0.1254</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
                <configuration>
                    <testFailureIgnore>false</testFailureIgnore>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/resources/docker</directory>
                                    <includes>
                                        <include>Dockerfile</include>
                                        <include>startup.sh</include>
                                        <include>sshd_config</include>
                                        <include>ssh_setup.sh</include>
                                        <include>proxy-intranet-ref-app.conf</include>
                                        <include>nginx-rproxy-secure.conf</include>
                                    </includes>
                                </resource>
                                <resource>
                                    <directory>src/main/resources/printTemplates</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</version>
                <configuration>
                    <includes>
                        <include>**/com/sams/clubops/**</include>
                    </includes>
                    <excludes>
                        <exclude>*</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>default-instrument</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                    </execution>

<!--                    Restores original classes as they were before offline instrumentation.-->
                    <execution>
                        <id>default-restore-instrumented-classes</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                    </execution>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.50</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.coveo</groupId>
                <artifactId>fmt-maven-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <displayLimit>10</displayLimit>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

While my test classes look like as following class

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

public class BayControllerTest {

  private static final int OK_STATUS = 200;
  private MockMvc mockMvc;
  @InjectMocks private BayController bayController;
  @Mock private BayService bayService;

  @BeforeEach
  public void init() {
    MockitoAnnotations.openMocks(this);
    mockMvc = MockMvcBuilders.standaloneSetup(bayController).build();
  }

  @Test
  public void testFetchBayInfo_Success() throws Exception {
    Mockito.when(bayService.findBayInfo(anyLong())).thenReturn(buildBayBOList());
    MvcResult mvcResult =
        mockMvc
            .perform(MockMvcRequestBuilders.get("/appointment/bayInfo/4969"))
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    assertEquals(OK_STATUS, status);
    assertEquals(SUCCESS_CODE, JSONObject.parseObject(content).get("code"));
  }

  @Test
  public void testFetchBayInfo_Failure() throws Exception {
    Mockito.when(bayService.findBayInfo(anyLong())).thenThrow(ApplicationException.class);
    MvcResult mvcResult =
        mockMvc
            .perform(MockMvcRequestBuilders.get("/appointment/bayInfo/4969"))
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    assertEquals(OK_STATUS, status);
    assertEquals(ERROR_CODE, JSONObject.parseObject(content).get("code"));
  }

  @Test
  public void testReloadBayInfo_Success() throws Exception {
    Mockito.doReturn(buildBayInfo()).when(bayService).refreshBayInfo(Mockito.anyBoolean());
    MvcResult mvcResult =
        mockMvc
            .perform(MockMvcRequestBuilders.post("/appointment/reloadBayInfo"))
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    assertEquals(OK_STATUS, status);
    assertEquals(SUCCESS_CODE, JSONObject.parseObject(content).get("code"));
  }

  @Test
  public void testReloadBayInfo_Failure() throws Exception {
    Mockito.doThrow(new ApplicationException())
        .when(bayService)
        .refreshBayInfo(Mockito.anyBoolean());
    MvcResult mvcResult =
        mockMvc
            .perform(MockMvcRequestBuilders.post("/appointment/reloadBayInfo"))
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
    int status = mvcResult.getResponse().getStatus();
    String content = mvcResult.getResponse().getContentAsString();
    assertEquals(OK_STATUS, status);
    assertEquals(ERROR_CODE, JSONObject.parseObject(content).get("code"));
  }

  private Map<Long, List<ClubBayBO>> buildBayInfo() {

    Map<Long, List<ClubBayBO>> bayMap = new HashMap<>();
    List<ClubBayBO> bayList = new ArrayList<>();
    bayList.add(buildBayBO());
    bayMap.put((long) 4969, bayList);
    return bayMap;
  }

  private ClubBayBO buildBayBO() {
    ClubBayBO bayBO = new ClubBayBO();
    bayBO.setBayId(1L);
    bayBO.setBayNbr(100);
    bayBO.setClubNbr(4969);
    bayBO.setDescription("This is the bay for Bentonville club");
    bayBO.setBayAvailable(true);
    return bayBO;
  }

  private List<ClubBayBO> buildBayBOList() {
    List<ClubBayBO> bayBoList = new ArrayList<>();
    bayBoList.add(buildBayBO());
    return bayBoList;
  }
}

CodePudding user response:

Your project is apparently a Spring Boot application because your pom.xml file has spring-boot-starter-parent as the parent. Spring Boot specifies dependency and plugin versions that were curated to work with that Spring Boot version. By convention, a Spring Boot project typically uses the dependency and plugin versions specified by Spring Boot.

Your pom.xml file overrides the maven-surefire-plugin version that Spring Boot would have used. If you delete the element:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
        <testFailureIgnore>false</testFailureIgnore>
    </configuration>
</plugin>

then your project will execute the maven-surefire-plugin version capable of running JUnit 5 tests.

CodePudding user response:

I suspect you need to be running 2.22.x of the Surefire plugin to get JUnit 5 compatibility. See the JIRAs included in those releases and note particularly SUREFIRE-1330. Then ensure the POM includes a junit-vintage-engine dependency.

P.S. For folks wondering "why have both JUnit 4 and 5" - the JUnit documentation says it best:

...it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custom build test infrastructure to migrate to JUnit Jupiter.

Instead, JUnit provides a gentle migration path via a JUnit Vintage test engine which allows existing tests based on JUnit 3 and JUnit 4 to be executed using the JUnit Platform infrastructure.

This approach is wonderful for large legacy codebases that can't be upgraded big-bang.

  • Related