微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Boot - 在没有 Spring 上下文的情况下测试实用程序类

如何解决Spring Boot - 在没有 Spring 上下文的情况下测试实用程序类

我有一个实用程序包,我想将它与同一项目中的 Spring boot 应用程序结合使用,而不是将它们分开存储。

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

  • 它们只是涉及对 resources 文件夹中存储的文件进行一些 JSON 解析的类 - 本质上,它们完全独立于 Spring 应用程序(它们不是服务,不需要它们被嘲笑,只是标准的实用程序类),尽管它们本身没有多大用处。

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

mvn test

我遇到了:

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

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

如果有帮助,这是我的 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 测试没有运行,因为最新的 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。