java - 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.
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.
- 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.
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
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.
Here's my pom.xml
if that helps:
<?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.
解决方案
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.
Recommended action is to upgrade to junit5.
If you want to stay on junit4, you can add this dependency so that legacy tests will be run:
<!--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>
推荐阅读
- python - 如何基于pyproject.toml构建python项目
- c# - 获取状态:404,statusText:“未找到”,url ...在asp.net中使用核心webApi?
- .htaccess - Htaccess 重写/隐藏查询字符串
- php - 我收到此错误“无法将 `string` 类型的值转换为整数”
- c# - 在 WCF 中使用 EntityFramework 加载子实体
- python - 如何读取从 jinja 2 发送的烧瓶中的 .csv 文件
- android - 同级视图可防止 recyclerview 在可点击时滚动
- woocommerce - 努力在 woocommerce 商店子类别中返回自定义字段
- python - 在张量流中根据条件减少总和
- hibernate - 部署在 Weblogic 上的 SpringBoot 应用程序 - Hibernate 日志记录