[英]Maven does not find JUnit tests to run
I have a maven program, it compiles fine.我有一个 maven 程序,它编译得很好。 When I run
mvn test
it does not run any tests (under TESTs header says There are no tests to run.
).当我运行
mvn test
它不运行任何测试(在 TESTs header 下说There are no tests to run.
)。
I've recreated this problem with a super simple setup which I will include below as well as the output when run with -X
.我用一个超级简单的设置重新创建了这个问题,我将在下面包括它以及使用
-X
运行时的 output。
The unit tests run fine from eclipse (both with its default junit package and when I instead include the junit.jar downloaded by maven).单元测试从 eclipse 开始运行良好(均使用其默认值 junit package,而当我改为包括由 maven 下载的 junit.jar 时)。 Also mvn
test-compile
correctly creates the class under test-classes. mvn
test-compile
也正确地在测试类下创建了 class。 I am running this on OSX 10.6.7 with Maven 3.0.2 and java 1.6.0_24.我在 OSX 10.6.7 上使用 Maven 3.0.2 和 java 1.6.0_24 运行它。
Here is the directory structure:这是目录结构:
/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java
pom.xml: 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my_group</groupId>
<artifactId>my_program</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>My Program</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
ClassUnderTest.java: ClassUnderTest.java:
public class ClassUnderTest {
public int functionUnderTest(int n) {
return n;
}
}
ClassUnderTestTests.java: ClassUnderTestTests.java:
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ClassUnderTestTests {
private ClassUnderTest o;
@Before
public void setUp() {
o = new ClassUnderTest();
}
@Test
public void testFunctionUnderTest_testCase1() {
Assert.assertEquals(1, o.functionUnderTest(1));
}
@Test
public void testFunctionUnderTest_testCase2() {
Assert.assertEquals(2, o.functionUnderTest(2));
}
}
End of mvn -X test: mvn -X 测试结束:
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-surefire-plugin:2.7.1, parent: sun.misc.Launcher$AppClassLoader@5224ee]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test' with basic configurator -->
[DEBUG] (s) basedir = /Users/aaron/Programs/my_program
[DEBUG] (s) childDelegation = false
[DEBUG] (s) classesDirectory = /Users/aaron/Programs/my_program/target/classes
[DEBUG] (s) disableXmlReport = false
[DEBUG] (s) enableAssertions = true
[DEBUG] (s) forkMode = once
[DEBUG] (s) junitArtifactName = junit:junit
[DEBUG] (s) localRepository = id: local
url: file:///Users/aaron/.m2/repository/
layout: none
[DEBUG] (f) parallelMavenExecution = false
[DEBUG] (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.7.1:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.7.1:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.7.1:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.0.5:compile, junit:junit=junit:junit:jar:3.8.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
[DEBUG] (s) printSummary = true
[DEBUG] (s) project = MavenProject: my_group:my_program:1.0-SNAPSHOT @ /Users/aaron/Programs/my_program/pom.xml
[DEBUG] (s) projectArtifactMap = {junit:junit=junit:junit:jar:4.8.1:test}
[DEBUG] (s) redirectTestOutputToFile = false
[DEBUG] (s) remoteRepositories = [ id: central
url: http://repo1.maven.org/maven2
layout: default
snapshots: [enabled => false, update => daily]
releases: [enabled => true, update => never]
]
[DEBUG] (s) reportFormat = brief
[DEBUG] (s) reportsDirectory = /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@dfbb43
[DEBUG] (s) skip = false
[DEBUG] (s) skipTests = false
[DEBUG] (s) testClassesDirectory = /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] (s) testFailureIgnore = false
[DEBUG] (s) testNGArtifactName = org.testng:testng
[DEBUG] (s) testSourceDirectory = /Users/aaron/Programs/my_program/src/test/java
[DEBUG] (s) trimStackTrace = true
[DEBUG] (s) useFile = true
[DEBUG] (s) useManifestOnlyJar = true
[DEBUG] (s) workingDirectory = /Users/aaron/Programs/my_program
[DEBUG] -- end configuration --
[INFO] Surefire report directory: /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] Setting system property [user.dir]=[/Users/aaron/Programs/my_program]
[DEBUG] Setting system property [localRepository]=[/Users/aaron/.m2/repository]
[DEBUG] Setting system property [basedir]=[/Users/aaron/Programs/my_program]
[DEBUG] Using JVM: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.7.1:test (selected for test)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:test (selected for test)
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.7.1/surefire-junit4-2.7.1.jar Scope: test
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: test
[DEBUG] Test Classpath :
[DEBUG] /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] /Users/aaron/Programs/my_program/target/classes
[DEBUG] /Users/aaron/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
Forking command line: /bin/sh -c cd /Users/aaron/Programs/my_program && /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/aaron/Programs/my_program/target/surefire/surefirebooter6118081963679415631.jar /Users/aaron/Programs/my_program/target/surefire/surefire4887918564882595612tmp /Users/aaron/Programs/my_program/target/surefire/surefire9012255138269731406tmp
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.089s
[INFO] Finished at: Mon May 30 12:03:09 EDT 2011
[INFO] Final Memory: 7M/62M
[INFO] ------------------------------------------------------------------------
By default Maven uses the following naming conventions when looking for tests to run:默认情况下,Maven 在查找要运行的测试时使用以下命名约定:
Test*
*Test
*Tests
( has been added in Maven Surefire Plugin 2.20 ) *Tests
(已在 Maven Surefire Plugin 2.20 中添加)*TestCase
If your test class doesn't follow these conventions you should rename it or configure Maven Surefire Plugin to use another pattern for test classes.如果您的测试 class 不遵循这些约定,您应该重命名它或配置 Maven Surefire 插件以使用其他模式进行测试类。
I also found that the unit test code should put under the src/test/java
folder, it can not be recognized as test class if you put it under the main folder.我还发现单元测试代码应该放在
src/test/java
文件夹下,如果放在主文件夹下就不能识别为test class。 eg.例如。
Wrong错误的
/my_program/src/main/java/NotTest.java
Right正确的
/my_program/src/test/java/MyTest.java
UPDATE:更新:
Like @scottyseus said in the comments, starting from Maven Surefire 2.22.0 the following is sufficient:正如@scottyseus 在评论中所说,从 Maven Surefire 2.22.0 开始,以下内容就足够了:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
When using JUnit 5, i ran into the same problem.使用JUnit 5时,我遇到了同样的问题。 Maven Surefire needs a plugin to run JUnit 5 tests.
Maven Surefire 需要一个插件来运行 JUnit 5 个测试。 Add this to our
pom.xml
:将其添加到我们的
pom.xml
:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0-M1</version>
</dependency>
</dependencies>
</plugin>
Source: https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven资料来源: https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
UPDATE 2021 2021 年更新
The junit-platform-surefire-provider, which was originally developed by the JUnit team, was deprecated in JUnit Platform 1.3 and discontinued in 1.4.
最初由 JUnit 团队开发的 junit-platform-surefire-provider 在 JUnit 平台 1.3 中被弃用,并在 1.4 中停产。 Please use Maven Surefire's native support instead.
请改用 Maven Surefire 的原生支持。
Another thing that can cause Maven to not find the tests if if the module's packaging is not declared correctly.如果模块的包装未正确声明,则可能导致 Maven 找不到测试的另一件事。
In a recent case, someone had <packaging>pom</packaging>
and my tests never ran.在最近的一个案例中,有人有
<packaging>pom</packaging>
而我的测试从未运行过。 I changed it to <packaging>jar</packaging>
and now it works fine.我将其更改为
<packaging>jar</packaging>
,现在它可以正常工作了。
In my case it was adding the junit-vintage-engine which makes it compatible with older version of JUnit tests and can run them.在我的例子中,它添加了 junit-vintage-engine,这使得它与旧版本的 JUnit 测试兼容并且可以运行它们。 As I'm using JUnit 5.
因为我正在使用 JUnit 5。
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
If you created a Spring Boot
application using Spring Initializr , tests are running all right from Intellij Idea.如果您使用Spring Initializr创建了一个
Spring Boot
应用程序,则测试可以从 Intellij Idea 正常运行。 But, if try to run tests from a command-line:但是,如果尝试从命令行运行测试:
mvn clean test
You might have been surprised, that no tests were run at all.您可能会感到惊讶,因为根本没有运行任何测试。 I tried to add
surefire plugin
with no luck.我尝试添加
surefire plugin
,但没有成功。 The answer was simple: pom.xml
contained the following dependency:答案很简单:
pom.xml
包含以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
The exclusion, junit-vintage-engine
, is dedicated for keeping backward compatibility with JUnit 4.x
.排除
junit-vintage-engine
专用于保持与JUnit 4.x
的向后兼容性。 So, new versions of Spring Boot Initializr do not support it by default.所以,Spring Boot Initializr 的新版本默认不支持它。 After I removed the exclusion,
Maven
started to see project's tests.在我删除排除后,
Maven
开始看到项目的测试。
Also, check if your test classes directory (eg src/test/java) corresponds to directory listed in property <testSourceDirectory>
in your pom.xml under <build>
property.此外,检查您的测试类目录(例如 src/test/java)是否对应于
<build>
属性下的 pom.xml 中属性<testSourceDirectory>
中列出的目录。 Took me a while to find that.我花了一段时间才找到那个。
Many of these answers were quite useful to me in the past, but I would like to add an additional scenario that has cost me some time, as it may help others in the future:其中许多答案在过去对我来说非常有用,但我想添加一个额外的场景,这花费了我一些时间,因为它可能会在未来帮助其他人:
Make sure that the test classes and methods are public.确保测试类和方法是公开的。
My problem was that I was using an automatic test class/methods generation feature of my IDE (IntelliJ) and for some reason it created them as package-private.我的问题是我正在使用我的 IDE (IntelliJ) 的自动测试类/方法生成功能,并且由于某种原因它创建了它们作为包私有。 I find this to be easier to miss than one would expect.
我发现这比人们预期的更容易错过。
I struggle with this problem.我在这个问题上挣扎。 In my case I wasn't importing the right @Test annotation.
在我的例子中,我没有导入正确的@Test注释。
1) Check if the @Test is from org.junit.jupiter.api.Test (if you are using Junit 5). 1)检查@Test是否来自org.junit.jupiter.api.Test (如果你使用的是Junit 5)。
2) With Junit5 instead of @RunWith(SpringRunner.class)
, use @ExtendWith(SpringExtension.class)
2)使用 Junit5 而不是
@RunWith(SpringRunner.class)
,使用@ExtendWith(SpringExtension.class)
import org.junit.jupiter.api.Test;
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application.properties")
public class CotacaoTest {
@Test
public void testXXX() {
}
}
Maven will not run your tests if the project has <packaging>pom</packaging>
如果项目有
<packaging>pom</packaging>
Maven 将不会运行你的测试
You need to set the packaging to jar (or some other java artefact type) for the tests to run: <packaging>jar</packaging>
您需要将包装设置为 jar(或其他一些 java 工件类型)才能运行测试:
<packaging>jar</packaging>
Discovered if you prefix a test with 'Abstract' it will be ignored by default aswell.发现如果您在测试前加上“摘要”,默认情况下它也会被忽略。
Check that (for jUnit - 4.12 and Eclipse surefire plugin)检查(对于 jUnit - 4.12 和 Eclipse surefire 插件)
Following worked just fine for me in Junit 5以下在 Junit 5 中对我来说效果很好
https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
I also had similar issue, after exploring found that testng dependency is causing this issue.我也有类似的问题,在探索后发现 testng 依赖项导致了这个问题。 After removing the testng dependency from pom (as I dont need it anymore), it started to work fine for me.
从 pom 中删除 testng 依赖项后(因为我不再需要它),它开始对我有用。
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
In my case we are migration multimodule application to Spring Boot.在我的例子中,我们将多模块应用程序迁移到 Spring Boot。 Unfortunately maven didnt execute all tests anymore in the modules.
不幸的是 maven 不再执行模块中的所有测试。 The naming of the Test Classes didnt change, we are following the naming conventions.
测试类的命名没有改变,我们遵循命名约定。
At the end it helped, when I added the dependency surefire-junit47
to the plugin maven-surefire-plugin
.最后,当我将依赖
surefire-junit47
添加到插件maven-surefire-plugin
时,它有所帮助。 But I could not explain, why, it was trial and error:但我无法解释,为什么,这是反复试验:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${maven-surefire-plugin.version}</version>
</dependency>
</dependencies>
The Maven Surefire plugin supports several test frameworks. Maven Surefire 插件支持多种测试框架。 It tries to autodetect which framework you are using, then looks for tests written using that framework.
它会尝试自动检测您正在使用的框架,然后查找使用该框架编写的测试。 If that autodetection is confused, and chooses the wrong framework, the second stage will not find your tests.
如果那个自动检测被混淆了,并且选择了错误的框架,第二阶段将找不到你的测试。
The autodetection works by scanning the classpath for the presence of significant "driver" classes for the test frameworks it supports.自动检测的工作原理是扫描类路径以查找其支持的测试框架是否存在重要的“驱动程序”类。 Therefore the autodetection can go wrong if your POM, or a depended on module, has an incorrect dependency on one of those "driver" classes.
因此,如果您的 POM 或依赖模块对其中一个“驱动程序”类具有不正确的依赖性,则自动检测可能会出错 go。
At present (2020), a particular problem is the difference between JUnit 4 and JUnit 5. The Surefire plugin treats them as different frameworks.目前(2020),一个特殊的问题是 JUnit 4 和 JUnit 5 之间的区别。Surefire 插件将它们视为不同的框架。 But because of the similarity in the package names, a project can have a dependency on the wrong framework but seem OK to a casual inspection.
但是由于 package 名称的相似性,一个项目可能依赖于错误的框架,但在不经意的检查中似乎没问题。
In particular, beware that junit-platform-console
is for JUnit 5, but junit-platform-runner
is for JUnit 4. If your project has a dependency on the latter, Surefire will not run your JUnit 5 tests.特别要注意
junit-platform-console
适用于 JUnit 5,而junit-platform-runner
适用于 JUnit 4。如果您的项目依赖于后者,Surefire 将不会运行您的 JUnit 5 测试。
If your test class name does not follow the standard naming convention (as highlighted by @axtavt above), you need to add the pattern/class name in the pom.xml
in order to Maven pick the test -如果您的测试 class 名称不遵循标准命名约定(如上面@axtavt 突出显示的那样),您需要在
pom.xml
中添加模式/类名称,以便 Maven 选择测试 -
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*_UT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
...
Here's the exact code I had to add to my pom.xml:这是我必须添加到我的 pom.xml 中的确切代码:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
And here's my dependencies:这是我的依赖项:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<version>2.0M10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.2.0-M1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0-M1</version>
</dependency>
</dependencies>
If you have a shared Java / Groovy application and all you have are Groovy unit tests, then Maven won't find any tests.如果您有一个共享的 Java / Groovy 应用程序,并且您只有 Groovy 单元测试,那么 Maven 将找不到任何测试。 This can be fixed by adding one unit test under src/test/java.
这可以通过在 src/test/java 下添加一个单元测试来解决。
/my_program/src/test/java/ClassUnderTestTests.java
should be应该
/my_program/src/test/java/ClassUnderTestTest.java
The Maven finds those ends Test or starts with Test to run automatically. Maven 找到那些结束测试或开始测试自动运行。
However, you can using但是,您可以使用
mvn surefire:test -Dtest=ClassUnderTestTests.java
to run your tests.运行你的测试。
I faced the same issue, it resolved by below change in pom.xml:我遇到了同样的问题,它通过 pom.xml 中的以下更改解决了:
<build>
<testSourceDirectory>test</testSourceDirectory>
... ...
changed to:变成:
<build>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
One more tip (in addition to the previous answers):还有一个提示(除了之前的答案):
In Eclipse, go to your project's Properties > click Run/Debug Settings
:在 Eclipse, go 到你项目的 Properties > 点击
Run/Debug Settings
:
"This page allows you to manage launch configurations with the currently selected resource"
“此页面允许您使用当前选定的资源管理启动配置”
In there you can add (New...) or remove (Delete) any JU (JUnit) tests you have in your project (under the src/test/java
folder, or course).在那里,您可以添加(新建...)或删除(删除)项目中的任何 JU (JUnit) 测试(在
src/test/java
文件夹或课程下)。
In my case, my parent pom had a parent:就我而言,我的父母 pom 有一个父母:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>some version</version>
<relativePath/>
</parent>
After changing to importing a spring pom:更改为导入 spring pom 后:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>some version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
My unit tests started to run我的单元测试开始运行
Probably not a common mistake but in addition to @João Matos 's answer.可能不是一个常见的错误,但除了@João Matos的回答。 Besides the requirement of your methods being public:
除了公开您的方法的要求外:
You're methods also should be void
and of course have the @Test
annotation.你的方法也应该是
void
的,当然有@Test
注释。
This won't work:这行不通:
@Test
public Integer fooBarTest() {
// omitted
}
It must return a void
:它必须返回一个
void
:
@Test
public void fooBarTest() {
// omitted
}
Another reason for not running the test cases happened to me - I had a property named "test" for completely different purposes, but it interfered with the surefire plugin.另一个不运行测试用例的原因发生在我身上——我有一个名为“test”的属性用于完全不同的目的,但它干扰了 surefire 插件。 Thus, please check your POMs for:
因此,请检查您的 POM:
<properties>
<test>.... </test>
...
</properties>
and remove it.并将其删除。
If you have written your tests in JUnit 4 and added JUnit 5 dependencies to the surefire plugin, your tests will not run.如果您在 JUnit 4 中编写了测试并向 surefire 插件添加了 JUnit 5 依赖项,您的测试将不会运行。
In that case, just comment JUnit 5 dependencies from surefire plugin:在这种情况下,只需注释 JUnit 来自 surefire 插件的 5 个依赖项:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<!--<dependencies>-->
<!--<dependency>-->
<!--<groupId>org.junit.platform</groupId>-->
<!--<artifactId>junit-platform-surefire-provider</artifactId>-->
<!--<version>1.0.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.junit.jupiter</groupId>-->
<!--<artifactId>junit-jupiter-engine</artifactId>-->
<!--<version>${junit.version}</version>-->
<!--</dependency>-->
<!--</dependencies>-->
</plugin>
Such problem might occur when you use surfire plugin 3.x.x+ with JUnit5 and by mistake annotate the test class with @Test
annotation from JUnit4.当您将 surfire 插件 3.x.x+ 与 JUnit5 一起使用并错误地使用来自 JUnit4 的
@Test
注释对测试 class 进行注释时,可能会出现此类问题。
Use: org.junit.jupiter.api.Test
(JUnit5) instead of org.junit.Test
(Junit4)使用:
org.junit.jupiter.api.Test
(JUnit5) 而不是org.junit.Test
(Junit4)
NOTE: this might be hard to notice as the IDE might run this wihout problems just as JUnit4 test.注意:这可能很难被注意到,因为 IDE 可能会像 JUnit4 测试一样运行而不会出现问题。
Another easily overlooked problem I recently experienced - Ensure your Test class' file has the.java extension.我最近遇到的另一个容易被忽视的问题 - 确保您的测试类文件具有 .java 扩展名。 If there's no tests to compile, there are no tests to run
如果没有要编译的测试,就没有要运行的测试
junitArtifactName
might also be the case if the JUnit in use isn't the standard ( junit:junit
) but for instance...如果使用的 JUnit 不是标准的(
junit:junit
),那么junitArtifactName
也可能是这种情况,但例如......
<dependency>
<groupId>org.eclipse.orbit</groupId>
<artifactId>org.junit</artifactId>
<version>4.11.0</version>
<type>bundle</type>
<scope>test</scope>
</dependency>
In case someone has searched and I do not solve it, I had a library for different tests:如果有人搜索过但我没有解决,我有一个用于不同测试的库:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${org.junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
When I installed junit everything worked, I hope and help this:当我安装 junit 一切正常时,我希望并帮助这个:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
I have a maven program, it compiles fine.我有一个 maven 程序,它编译得很好。 When I run
mvn test
it does not run any tests (under TESTs header says There are no tests to run.
).当我运行
mvn test
时,它没有运行任何测试(在 TESTs header 下说There are no tests to run.
)。
I've recreated this problem with a super simple setup which I will include below as well as the output when run with -X
.我已经用一个超级简单的设置重新创建了这个问题,我将在下面包括使用
-X
运行时的 output 。
The unit tests run fine from eclipse (both with its default junit package and when I instead include the junit.jar downloaded by maven). The unit tests run fine from eclipse (both with its default junit package and when I instead include the junit.jar downloaded by maven). Also mvn
test-compile
correctly creates the class under test-classes.此外 mvn
test-compile
在测试类下正确创建 class 。 I am running this on OSX 10.6.7 with Maven 3.0.2 and java 1.6.0_24.我在 OSX 10.6.7 上使用 Maven 3.0.2 和 java 1.6.0_24 运行它。
Here is the directory structure:这是目录结构:
/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java
pom.xml: 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my_group</groupId>
<artifactId>my_program</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>My Program</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
ClassUnderTest.java:类UnderTest.java:
public class ClassUnderTest {
public int functionUnderTest(int n) {
return n;
}
}
ClassUnderTestTests.java:类UnderTestTests.java:
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ClassUnderTestTests {
private ClassUnderTest o;
@Before
public void setUp() {
o = new ClassUnderTest();
}
@Test
public void testFunctionUnderTest_testCase1() {
Assert.assertEquals(1, o.functionUnderTest(1));
}
@Test
public void testFunctionUnderTest_testCase2() {
Assert.assertEquals(2, o.functionUnderTest(2));
}
}
End of mvn -X test: mvn -X 测试结束:
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-surefire-plugin:2.7.1, parent: sun.misc.Launcher$AppClassLoader@5224ee]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test' with basic configurator -->
[DEBUG] (s) basedir = /Users/aaron/Programs/my_program
[DEBUG] (s) childDelegation = false
[DEBUG] (s) classesDirectory = /Users/aaron/Programs/my_program/target/classes
[DEBUG] (s) disableXmlReport = false
[DEBUG] (s) enableAssertions = true
[DEBUG] (s) forkMode = once
[DEBUG] (s) junitArtifactName = junit:junit
[DEBUG] (s) localRepository = id: local
url: file:///Users/aaron/.m2/repository/
layout: none
[DEBUG] (f) parallelMavenExecution = false
[DEBUG] (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.7.1:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.7.1:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.7.1:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.0.5:compile, junit:junit=junit:junit:jar:3.8.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
[DEBUG] (s) printSummary = true
[DEBUG] (s) project = MavenProject: my_group:my_program:1.0-SNAPSHOT @ /Users/aaron/Programs/my_program/pom.xml
[DEBUG] (s) projectArtifactMap = {junit:junit=junit:junit:jar:4.8.1:test}
[DEBUG] (s) redirectTestOutputToFile = false
[DEBUG] (s) remoteRepositories = [ id: central
url: http://repo1.maven.org/maven2
layout: default
snapshots: [enabled => false, update => daily]
releases: [enabled => true, update => never]
]
[DEBUG] (s) reportFormat = brief
[DEBUG] (s) reportsDirectory = /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] (s) session = org.apache.maven.execution.MavenSession@dfbb43
[DEBUG] (s) skip = false
[DEBUG] (s) skipTests = false
[DEBUG] (s) testClassesDirectory = /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] (s) testFailureIgnore = false
[DEBUG] (s) testNGArtifactName = org.testng:testng
[DEBUG] (s) testSourceDirectory = /Users/aaron/Programs/my_program/src/test/java
[DEBUG] (s) trimStackTrace = true
[DEBUG] (s) useFile = true
[DEBUG] (s) useManifestOnlyJar = true
[DEBUG] (s) workingDirectory = /Users/aaron/Programs/my_program
[DEBUG] -- end configuration --
[INFO] Surefire report directory: /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] Setting system property [user.dir]=[/Users/aaron/Programs/my_program]
[DEBUG] Setting system property [localRepository]=[/Users/aaron/.m2/repository]
[DEBUG] Setting system property [basedir]=[/Users/aaron/Programs/my_program]
[DEBUG] Using JVM: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-junit4:jar:2.7.1:test (selected for test)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:test (selected for test)
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.7.1/surefire-junit4-2.7.1.jar Scope: test
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: test
[DEBUG] Test Classpath :
[DEBUG] /Users/aaron/Programs/my_program/target/test-classes
[DEBUG] /Users/aaron/Programs/my_program/target/classes
[DEBUG] /Users/aaron/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG] org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG] org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
Forking command line: /bin/sh -c cd /Users/aaron/Programs/my_program && /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/aaron/Programs/my_program/target/surefire/surefirebooter6118081963679415631.jar /Users/aaron/Programs/my_program/target/surefire/surefire4887918564882595612tmp /Users/aaron/Programs/my_program/target/surefire/surefire9012255138269731406tmp
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.089s
[INFO] Finished at: Mon May 30 12:03:09 EDT 2011
[INFO] Final Memory: 7M/62M
[INFO] ------------------------------------------------------------------------
Faced this issue while working on a spring boot project setup with Junit4.在使用 Junit4 设置 spring 引导项目时遇到此问题。 Jacoco maven plugin always returned 0.00 test coverage and maven surefire plugin ran 0 tests.
Jacoco maven 插件始终返回 0.00 测试覆盖率,maven surefire 插件运行 0 次测试。
Removed junit-vintage-engine and surefire-junit47 dependency and added the following dependencies to run Junit 5 unit tests.删除了 junit-vintage-engine 和 surefire-junit47 依赖项并添加了以下依赖项以运行 Junit 5 个单元测试。
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
Adding version lower than 5.6.2 led to ScriptEvaluationException.添加低于 5.6.2 的版本导致 ScriptEvaluationException。
Also using @ExtendWith(SpringExtension.class) instead of@RunWith(SpringRunner.class)还使用@ExtendWith(SpringExtension.class) 而不是@RunWith(SpringRunner.class)
I tried most of the answers in this thread but I got it working only when I did the following to my pom.xml
.我尝试了这个线程中的大部分答案,但只有当我对我的
pom.xml
执行以下操作时,我才让它工作。
It had the org.junit.jupiter
as a seperate dependency它有
org.junit.jupiter
作为一个单独的依赖
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
But then I moved the same inside the maven-surefire-plugin
plugin and the mvn clean install/test
started detecting my test files which was under src/test/java
但是后来我在
maven-surefire-plugin
插件中移动了相同的内容,并且mvn clean install/test
开始检测我在src/test/java
下的测试文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<dependencies>
<!-- moved here -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
</plugin>
Using Surefire plugin you might want to include all the files for testing:使用 Surefire 插件,您可能希望包含所有用于测试的文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<includes>
<include>*</include>
</includes>
</configuration>
</plugin>
this way you can get all your files tested, not only the ones that include the pattern that Surefire requires.这样您就可以测试所有文件,而不仅仅是那些包含 Surefire 所需模式的文件。
Please make sure to use junit-jupiter
and NOT junit-jupiter-api
(which I used accidentally)请确保使用
junit-jupiter
而不是junit-jupiter-api
(我不小心用到了)
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.