【发布时间】:2019-02-27 14:40:08
【问题描述】:
在我的 Spring 应用程序中,我有正确命名的测试:
src
-main
-test
-java
-com.mypackage.sth
-utils
SomethingTest.java
我可以直接从 IDE intellij 运行它,但是当我从 maven 窗口中选择它时:
我正在获取信息:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
在我的pom.xml 文件中,我有这个部分:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
我上面还加了一个依赖:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
但测试仍然没有运行。我该如何解决?这里的主要问题是什么?
----- 编辑 我完全按照 Naor Tedgi 在第一个答案中的建议做了,但是在添加之后:
<sourceDirectory>src/main/..</sourceDirectory>
<testSourceDirectory>src/test/..</testSourceDirectory>
到<build> 标记,我的项目结构变得疯狂,这样做之后看起来像这样:
src
-main
-test.java.com.mypackage.sth
-utils
SomethingTest.java
并且测试类中的所有符号都是不可解析的。现在,当我尝试运行测试时,它们运行了!所以这是一件好事,但是有很多很多错误,例如
package org.junit does not exist
等等
--- 编辑 v2
好的,我编辑了我的 pom.xml,现在它在 <build> 中包含这两行:
<sourceDirectory>src/main/java/</sourceDirectory>
<testSourceDirectory>src/test/java/</testSourceDirectory>
错误消失了,项目是可编译的,但它仍然没有帮助 - 测试没有运行
这是mvn test 的输出:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myProject 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ projectName ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ projectName ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ projectName ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\path\to\myProject\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ projectName ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ projectName ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.181 s
[INFO] Finished at: 2019-02-27T17:16:29+01:00
[INFO] Final Memory: 22M/224M
[INFO] ------------------------------------------------------------------------
【问题讨论】:
-
请添加测试文件示例,并通过单击 ctrl+shift+f10 来验证是否可以手动运行测试
-
我可以手动运行它们,当我在一个测试文件中并点击 ctrl+shift+f10 时,当前突出显示的测试就会运行。我的测试相当简单,
@Test public void should...Test() { //when something //then assertEquals(x, y); }等。我认为测试类本身没有问题,而是在某个地方配置了万无一失
标签: maven maven-surefire-plugin