在 IDE 的 Maven 测试过程中测试不是 运行(使用 surefire)
tests are not run during maven test procedure from IDE (using surefire)
在我的 Spring
应用程序中,我有正确命名的测试:
src
-main
-test
-java
-com.mypackage.sth
-utils
SomethingTest.java
我可以 运行 直接从 IDE intellij
,但是当我从 maven 选择它时 window:
我正在获取信息:
[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] ------------------------------------------------------------------------
首先,将构建标记之外的 Junit 提取到 dependencies
然后将此行添加到构建
<sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
<testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>
应该看起来像这样
<project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
<testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>
<plugins>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
在我的 Spring
应用程序中,我有正确命名的测试:
src
-main
-test
-java
-com.mypackage.sth
-utils
SomethingTest.java
我可以 运行 直接从 IDE intellij
,但是当我从 maven 选择它时 window:
我正在获取信息:
[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] ------------------------------------------------------------------------
首先,将构建标记之外的 Junit 提取到 dependencies
然后将此行添加到构建
<sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
<testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>
应该看起来像这样
<project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/{PATH TO SRC}</sourceDirectory>
<testSourceDirectory>src/test/{PATH TO TESTS}</testSourceDirectory>
<plugins>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>