Home > front end >  Unit testing java maven
Unit testing java maven

Time:10-04

I have a maven project that looks like this:

enter image description here

If I run in the terminal:

mvn test

it will build the application and run the 3 tests:

SocialMultiplicationApplicationTests

MultiplicationServiceTest

RandomGeneratorServiceTest

but not RandomGeneratorServiceImplTest.

If I try to explicitly run this class:

mvn test -Dtest=RandomGeneratorServiceImplTest

I get:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) 
on project social-multiplication: No tests were executed!  (Set -DfailIfNoTests=false to ignore this error.) -> [Help 1]

Here's 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>microservices.book</groupId>
    <artifactId>social-multiplication</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>social-multiplication</name>
    <description>Social Multiplication App</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <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>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Here's the test class that won't run:

package microservices.book.multiplication.service;


import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class RandomGeneratorServiceImplTest {

    private RandomGeneratorServiceImpl randomGeneratorServiceImp;

    @Before
    public void setUp(){
        randomGeneratorServiceImp = new RandomGeneratorServiceImpl();
    }

    @Test
    public void generateRandomFactorIsBetweenExpectedLimits() {
        List<Integer> randomFactors = IntStream.range(0,1000)
                .map(i -> randomGeneratorServiceImp.generateRandomFactor())
                .boxed()
                .collect(Collectors.toList());

        for(Integer i: randomFactors){
            assertThat(i).isIn(
                    IntStream.range(11, 100).
                            boxed().collect(Collectors.toList()));
        }
    }
}

CodePudding user response:

Try adding this plugin to your build section in pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

CodePudding user response:

The reason lies in mixing JUnit 5 and JUnit 4. The maven-surefire-plugin picks only one strategy to execute tests and favors JUnit 5 in this case.

  1. Either replace the dependency to junit by junit-vintage-engine:
<!--        <dependency>-->
<!--            <groupId>junit</groupId>-->
<!--            <artifactId>junit</artifactId>-->
<!--            <scope>test</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <scope>test</scope>
        </dependency>
  1. Or adapt your tests to use JUnit 5 instead (as for example in RandomGeneratorServiceImplTest):
...

//import org.junit.Before;
//import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

...

public class RandomGeneratorServiceImplTest {

    ...

//    @Before
    @BeforeEach
    public void setUp(){
        ...
    }

    // @Test can stay as is, only import needs to be updated
    @Test
    public void generateRandomFactorIsBetweenExpectedLimits() {
        ...
    }
}
  • Related