问题描述
我有一个实用程序包,我想将它与同一项目中的 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>