Rakesh K
Rakesh K

Reputation: 8505

mvn test does not run unit tests in springboot project

I have unit tests in my springboot project in addition to the default application test that comes with the project bundle when I create the project from start.spring.io. When I run mvn test from command line, I see that only the default application tests are run but not the unit tests that I have written. However, I can run these tests from IntelliJ. I am using maven version 3.6.2 and maven surefire plugin version 2.22.2. Can someone let me know what I am missing here? Thanks.

Here's my test class:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BranchServiceUnitTest {

    @Autowired
    private BranchService branchService;

    @MockBean
    private BranchRepository branchRepository;

    @Test
    public void testAddNewBranch() {
        Branch testBranch = new Branch();
        testBranch.setBranchName("TestBranch");
        testBranch.setCity("TestCity");
        testBranch.setContactNumber("TestContactNumber");
        testBranch.setEmailId("TestEmailId");
        Mockito.when(branchRepository.save(testBranch)).thenReturn(testBranch);
        Branch addedBranch = branchService.addBranch(testBranch);
        assertThat(addedBranch.getCity()).isEqualTo("TestCity");
    }

    @Test
    public void findBranchById() {
        Branch testBranch = new Branch();
        testBranch.setId(1);
        testBranch.setBranchName("TestBranch");
        testBranch.setCity("TestCity");
        testBranch.setContactNumber("TestContactNumber");
        testBranch.setEmailId("TestEmailId");
        Mockito.when(branchRepository.findById(testBranch.getId())).thenReturn(java.util.Optional.of(testBranch));
        Branch foundBranch = branchService.getBranchById(1);
        assertThat(foundBranch.getId()).isEqualTo(1);
    }

    @Test
    public void testGetAllBranches() {
        Branch testBranch1 = new Branch();
        testBranch1.setId(1);
        testBranch1.setBranchName("TestBranch");
        testBranch1.setCity("TestCity");
        testBranch1.setContactNumber("TestContactNumber");
        testBranch1.setEmailId("TestEmailId");

        Branch testBranch2 = new Branch();
        testBranch2.setId(2);
        testBranch2.setBranchName("TestBranch");
        testBranch2.setCity("TestCity");
        testBranch2.setContactNumber("TestContactNumber");
        testBranch2.setEmailId("TestEmailId");

        List<Branch> branches = Arrays.asList(testBranch1,testBranch2);
        Mockito.when(branchRepository.findAll()).thenReturn(branches);
        assertThat(branches.size()).isEqualTo(2);
        assertThat(branches.get(0).getId()).isEqualTo(1);
    }
}

Following is my pom file:

<?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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rkasibha</groupId>
    <artifactId>rentabook</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rentabook</name>
    <description>Rent a book service</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>

Upvotes: 0

Views: 1314

Answers (3)

resatz
resatz

Reputation: 576

If your tests run successfully when ran alone, But not picked up during the

mvn test

Chances are that you might be using an Older Junit package in your tests. Usually the above weird scenario happens when there's a mix up in the Junit version.

If Junit5 is being used, please ensure that the package in imports is

import org.junit.jupiter.api

Upvotes: 0

Mark Bramnik
Mark Bramnik

Reputation: 42451

I think its a weird mix of JUnit 4 and Junit 5 that causes the issue:

  1. Spring boot 2.2.6 (I've used start.spring.io to generate a sample application) uses junit 5.
  2. On the other hand, your test is written with @RunWith which means that it uses junit 4 under the hood.
  3. The dependency:
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <scope>test</scope>
</dependency>

also seems suspicious - the spring-boot-starter-test already contains all the required dependencies on JUnit 5, so you don't need this one.

Now in terms of resolution, check out the default test that comes with this sample application (the one you've described in the question). The chances are that it uses JUnit 5 by itself, so you better migrate your test to JUnit 5 and rerun.

Upvotes: 1

Michael
Michael

Reputation: 44150

This looks spurious.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <includes>
            <include>**/*Test*.java</include>
        </includes>
    </configuration>
</plugin>

What do you hope to gain from using <include>**/*Test*.java</include>? I'm pretty certain the trailing * does not mean zero-or-more characters. It's 1 or more. Documentation

Are there specific classes in your test directory that you want to exclude? If not, I would remove the whole plugin. Surefire is already declared in Maven's implicit parent POM, with sensible defaults that will cover all of your tests. Declaring it yourself is both needlessly verbose and has actively broken something which works out of the box.

Upvotes: 0

Related Questions