Spring 启动 - 测试实用程序 类 没有 Spring 上下文

Spring Boot - Test utility classes without Spring context

我有一个实用程序包,我想将其与 Spring 引导应用程序结合在同一个项目中,而不是单独存储它们。

我想对这些根本不需要 Spring 上下文的内部实用程序 类 进行单元测试 - 我可以从单元测试中调用它们 类 所以启动应用程序是不必要的开销。

现在,我可以通过单击显示的测试图标在我的 IDE (IntelliJ) 中手动 运行 测试,但是,当我尝试使用 运行 它们时 [=22] =]

mvn test

我遇到了:

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

请注意,当实用程序包位于单独的包中(使用 mvn test)时,我可以 运行 junit 测试,但我想将它放在与应用程序。

如果有帮助,这是我的 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 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>

非常感谢任何帮助。

编辑

我的测试结构:

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

我想 运行 utility 包中的所有测试。

您没有提供足够的详细信息以使其可重现,但根据您提供的信息,我认为您的 junit 测试不是 运行ning,因为最新的 spring 将默认到 junit5,而不是 junit4,在这种情况下将默默地忽略 junit4 测试,除非您明确指定它们是 运行.

建议的操作是升级到 junit5。

如果你想继续使用 junit4,你可以添加这个依赖,这样遗留测试就会 运行:

   <!--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>