Maven/Surefire 找不到要运行的测试
据我所知,测试文件位置是正确的。
当我运行“mvn test”时,它找到四个名为 SomethingTest 的类(它们位于“test”文件夹中)。
但是,它会忽略任何 jUnit 测试(jUnit 4,用 @Test 注释)。
我该如何调试这个?
编辑 - 这可能与包含的 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)
尽管我在根 pom 中的第一个依赖项是 jUnit 4.8.1,但出于某种原因,包含了 jUnit 3.8.1。
编辑 2 - 好吧,这似乎不是答案。测试类路径包括正确的 jUnit (4) 和我的测试类。
编辑 3 - 我有名为 SomethingTester 的测试类。当我将其更改为 SomethingTest 时,它起作用了。我检查了 Surefire 的包含模式,确实它没有配置为捕获 Something Tester。哦。
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).
However, it ignores any of the jUnit tests (jUnit 4, annotated with @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"
[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.
Edit 2 - ok, this doesn't seem to be the answer. The Test Classpath includes the correct jUnit (4) and my test classes.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许这就是问题所在:
mvn -X 会打印一堆这些,所以你可以尝试弄清楚它是否是上面的内容 - 比如没有使用正确的 JUnit 版本(例如,当你从快速启动工件创建时,我认为默认的是 3.8.1),在 JUnit 之前的类路径中包含 TestNG。
编辑:我只是在一个简单的项目和上面链接中给出的类中尝试过这个,它工作得很好。我使用 junit 版本 4.8,这是我的项目中唯一的依赖项。只是为了确认,您正在使用 @org.junit.Test 注释测试方法,并且这些方法中有一些 org.junit.Assert.assertXXX 语句,对吗?
编辑2:要将junit更改为其他版本,请使用以下命令:
编辑3:您应该在测试类路径中包含类似的内容:
编辑4:好的,我刚刚使用maven快速启动工件创建了一个测试项目,添加了两个模块(还创建了包含快速启动工件),仅向父 pom 添加了源/目标 Java 版本和 junit:junit:4.8 依赖项。我只将其中一个测试更改为 JUnit4(另一个测试默认为 JUnit3,这就是快速启动生成的),父文件夹中的
mvn clean test
工作得很好。这很可能是项目设置问题 - 您能否检查您的项目是否正确连接(即模块指向父项目,父/子项目的组/工件/版本是否正确)。我想到的唯一的另一件事是清理你的 Maven 存储库(至少是 org/apache/maven),但我怀疑这会有帮助。
在一个更简单的项目上进行测试可能是明智的。
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.
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?
Edit 2: To change junit to some other version, use this:
Edit 3: You should have something like this in the test classpath:
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 test
from parent folder worked just fine.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.
It might be wise to test out on a simpler project.
完成icyrock.com的问题。如果没有任何测试类编译为 target/test-classes,请检查您的 pom 文件并确保打包不是“pom”。
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'.
检查 pout 您当前版本的 JUnit 是否在 Maven 存储库中仍然可用,否则,它将失败
此答案的当前版本 @time 是
Check pout if your current version of JUnit is still available in the Maven repository, otherwise, it would fail
The current version @time of this answer is