Java Maven/Surefire 找不到要运行的测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4174276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:20:49  来源:igfitidea点击:

Maven/Surefire finds no tests to run

javamaven-2junitsurefire

提问by ripper234

As far as I can tell, the test files location is correct.

据我所知,测试文件的位置是正确的。

When I run "mvn test", it finds four classes named SomethingTest (they are located in the 'test' folder).

当我运行“mvn test”时,它会找到四个名为SomethingTest的类(它们位于“test”文件夹中)。

However, it ignores any of the jUnit tests (jUnit 4, annotated with @Test).

但是,它会忽略任何 jUnit 测试(jUnit 4,用 @Test 注释)。

How do I debug this?

我该如何调试?

Edit- this is probablly related to wrong version of jUnit being included. I see this when running "mvn -X"

编辑- 这可能与包含的 jUnit 版本错误有关。我在运行“mvn -X”时看到了这个

[DEBUG] Retrieving parent-POM: org.codehaus.plexus:plexus:pom:1.0.4 for project: org.codehaus.plexus:plexus-containers:pom:1.0.3 from the repository.
[DEBUG]       org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:runtime (selected for runtime)
[DEBUG]         junit:junit:jar:3.8.1:runtime (selected for runtime)
[DEBUG]         org.codehaus.plexus:plexus-utils:jar:1.0.4:runtime (removed - nearer found: 1.4.1)
[DEBUG]         classworlds:classworlds:jar:1.1-alpha-2:runtime (selected for runtime)

Even though my first dependency in the root pom is on jUnit 4.8.1, for some reason jUnit 3.8.1 is being included.

尽管我在根 pom 中的第一个依赖项是在 jUnit 4.8.1 上,但出于某种原因,包含了 jUnit 3.8.1。

Edit 2- ok, this doesn't seem to be the answer. The Test Classpath includes the correct jUnit (4) and my test classes.

编辑 2- 好的,这似乎不是答案。测试类路径包括正确的 jUnit (4) 和我的测试类。

Edit 3- I had the test classes named SomethingTester. When I changed it to SomethingTest, it worked. I checked the include patterns for Surefire, and indeed it wasn't configured to catch Something Tester. Doh.

编辑 3- 我有一个名为 SomeTester 的测试类。当我将其更改为SomethingTest 时,它起作用了。我检查了 Surefire 的包含模式,确实它没有被配置为捕获某些测试程序。多。

回答by icyrock.com

Maybe this is the issue:

也许这就是问题所在:

mvn -X would print a bunch of these, so you can try to figure out if it's something from the above - like not using the right JUnit version (e.g. when you create from the quickstart artifact, I think the default is 3.8.1), having TestNG in the classpath before JUnit or so.

mvn -X 会打印一堆这些,所以你可以尝试找出它是否来自上面的东西 - 比如没有使用正确的 JUnit 版本(例如,当你从快速启动工件创建时,我认为默认值为 3.8.1) ,在 JUnit 之前的类路径中有 TestNG。

Edit: I just tried this in a simple project and the class given in the above link and it worked fine. I used junit version 4.8, that is the only dependency in my project. Just to confirm, you are annotating test methods with @org.junit.Test and there are some org.junit.Assert.assertXXX statements in these methods, correct?

编辑:我只是在一个简单的项目和上面链接中给出的类中尝试过这个,它工作正常。我使用了 junit 4.8 版,这是我项目中唯一的依赖项。只是为了确认,您正在使用@org.junit.Test 注释测试方法,并且这些方法中有一些 org.junit.Assert.assertXXX 语句,对吗?

Edit 2: To change junit to some other version, use this:

编辑 2:要将 junit 更改为其他版本,请使用:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8</version>
  <scope>test</scope>
</dependency>

Edit 3: You should have something like this in the test classpath:

编辑 3:你应该在测试类路径中有这样的东西:

[DEBUG] Test Classpath :
[DEBUG]   /home/icyrock/java/prb/target/test-classes
[DEBUG]   /home/icyrock/java/prb/target/classes
[DEBUG]   /home/icyrock/.m2/repository/junit/junit/4.8/junit-4.8.jar
[DEBUG]   /home/icyrock/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar
[DEBUG]   /home/icyrock/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar
[DEBUG]   /home/icyrock/.m2/repository/log4j/log4j/1.2.16/log4j-1.2.16.jar

Edit 4: OK, I just created a test project with maven quickstart artifact, added two modules (also created with quickstart artifact) inside, added source/target Java version and junit:junit:4.8 dependency to the parent pom only. I changed only one of the tests to JUnit4 (the other one is by default JUnit3, that's what quickstart generates), mvn clean testfrom parent folder worked just fine.

编辑 4:好的,我刚刚创建了一个带有 Maven 快速启动工件的测试项目,在里面添加了两个模块(也是使用快速启动工件创建的),仅向父 pom 添加了源/目标 Java 版本和 junit:junit:4.8 依赖项。我只将其中一个测试更改为 JUnit4(另一个默认是 JUnit3,这是快速入门生成的),mvn clean test从父文件夹工作得很好。

This is most likely a project setup issue - can you check your project is wired correctly (i.e. modules point to the parent, the group/artifact/versions of parent/child projects are correct). The only other thing that comes to my mind is cleaning your maven repository (at least org/apache/maven), but I doubt that would help.

这很可能是项目设置问题 - 您能否检查您的项目是否正确连接(即模块指向父项目,父/子项目的组/工件/版本是否正确)。我想到的唯一另一件事是清理您的 maven 存储库(至少 org/apache/maven),但我怀疑这会有所帮助。

It might be wise to test out on a simpler project.

在一个更简单的项目上进行测试可能是明智的。

回答by Raymond

To finish icyrock.com's question. If there aren't any test classes compiled to target/test-classes then check your pom file and ensure that the packaging isn't 'pom'.

完成icyrock.com的问题。如果没有任何测试类编译为目标/测试类,请检查您的 pom 文件并确保包装不是“pom”。