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