首页 > 解决方案 > Spring Boot - Test utility classes without Spring context

问题描述

I have a utility package which I want to combine with a Spring boot application in the same project, instead of storing them separately.

I would like to unit test these inner utility classes which do not require the Spring context at all - I can call them from the unit test classes so starting the application is an unnecessary overhead.

Now, I can manually run the tests in my IDE (IntelliJ) by clicking the test icon shown, however, when I try to run them using

mvn test

I am met with:

[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] 

Note that I can run the junit tests when the utility package is in a separate package (with mvn test) but I would like to house it under the same package as the application.

Here's my pom.xml if that helps:

<?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>project</groupId>
    <artifactId>project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>project</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.compiler.source>13</maven.compiler.source>
        <maven.compiler.target>13</maven.compiler.target>
        <start-class>package.application.Application</start-class>
    </properties>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>[3.2.2,)</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>[4.13.1,)</version>
            <scope>test</scope>
        </dependency>

        ... Other dependencies

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <start-class>package.application.Application</start-class>
                    <layout>ZIP</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Any help is greatly appreciated.

EDIT

My test structure:

test/java
    - utility
        - category1
            - ParserTest.java
        - category2
            - SomeOtherTest.java
    - application

And I would like to run all the tests in the utility package.

标签: javaspringspring-bootmavenjunit4

解决方案


You haven't provided enough details for this to be reproducable, but based on information you've given, I think your junit tests are not running since the latest spring will default to junit5, not junit4, and in this case will silently ignore junit4 tests unless you've explicitly specified them to be run.

Recommended action is to upgrade to junit5.

If you want to stay on junit4, you can add this dependency so that legacy tests will be run:

   <!--JUnit Jupiter Engine to depend on the JUnit4 engine and JUnit 4 API  -->
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>5.1.0</version>
    </dependency>

推荐阅读