简体   繁体   English

Spring 引导 - 没有 Spring 上下文的测试实用程序类

[英]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.我有一个实用程序 package,我想将它与 Spring 引导应用程序结合在同一个项目中,而不是单独存储它们。

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

  • They're just classes involving some JSON parsing on files stored in the resources folder - in essence, they're completely independent of the Spring application (they're not services and there is no need for them to be mocked, just standard utility classes), albeit not of much use on their own.它们只是涉及一些 JSON 解析存储在resources文件夹中的文件的类 - 从本质上讲,它们完全独立于 Spring 应用程序(它们不是服务,不需要模拟它们,只是标准实用程序类),尽管它们本身并没有多大用处。

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

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. 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:这是我的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>

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.我想在utility package 中运行所有测试。

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.您没有提供足够的详细信息来重现此问题,但根据您提供的信息,我认为您的 junit 测试没有运行,因为最新的 spring 将默认为 junit5,而不是 junit4,在这种情况下将默默地忽略 junit4测试,除非您明确指定要运行它们。

Recommended action is to upgrade to junit5.推荐的操作是升级到junit5。

If you want to stay on junit4, you can add this dependency so that legacy tests will be run:如果您想继续使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 JUnit 测试类中使用属性值而不加载整个 Spring 引导应用程序上下文 - Using property values in JUnit test classes without load whole Spring Boot application context 如何使用 JDBC 在 spring 引导测试类上创建事务上下文? - How to create a transactional context on spring boot test classes using JDBC? Spring Boot 测试:为每个测试加载上下文? - Spring boot test: context loaded for every test? Spring Boot 测试类可以重用应用程序上下文以加快测试运行速度吗? - Can Spring Boot test classes reuse application context for faster test run? Spring 引导测试上下文未加载:Flyway 和 Spring 引导数据/休眠 - Spring Boot test context not loading: Flyway and Spring Boot Data/Hibernate 如何在 Spring Boot Test 和 MockMvc 中创建上下文? - How to create a context in Spring Boot Test and MockMvc? 无法在 Spring Boot 测试中加载上下文属性 - Cannot load context properties in a Spring Boot test 在 HttpServletRequest 上下文之外测试 Spring Boot WebClient - test a Spring Boot WebClient outside of a HttpServletRequest context Spring Boot集成测试-如何在单个上下文中运行多个测试类? - Spring Boot Integration Tests - How to run multiple test classes with a single context? Spring Boot测试API和存储库的多个类 - Spring boot Test multiple classes for API and repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM