简体   繁体   English

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

[英]Maven/Surefire finds no tests to run

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.编辑- 这可能与包含的 jUnit 版本错误有关。 I see this when running "mvn -X"我在运行“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.编辑 2 - 好的,这似乎不是答案。 The Test Classpath includes the correct jUnit (4) and my test classes.测试类路径包括正确的 jUnit (4) 和我的测试类。

Edit 3 - I had the test classes named SomethingTester.编辑 3 - 我有一个名为 SomeTester 的测试类。 When I changed it to SomethingTest, it worked.当我将其更改为SomethingTest 时,它起作用了。 I checked the include patterns for Surefire, and indeed it wasn't configured to catch Something Tester.我检查了 Surefire 的包含模式,确实它没有被配置为捕获某些测试程序。 Doh.多。

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 (eg 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.我使用了 junit 4.8 版,这是我项目中唯一的依赖项。 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?只是为了确认,您正在使用@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.编辑 4:好的,我刚刚创建了一个带有 Maven 快速启动工件的测试项目,在里面添加了两个模块(也是使用快速启动工件创建的),仅向父 pom 添加了源/目标 Java 版本和 junit:junit:4.8 依赖项。 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.我只将其中一个测试更改为 JUnit4(默认情况下,另一个是 JUnit3,这是快速入门生成的),来自父文件夹的mvn clean test工作得很好。

This is most likely a project setup issue - can you check your project is wired correctly (ie 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.在一个更简单的项目上进行测试可能是明智的。

To finish icyrock.com's question.完成icyrock.com的问题。 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'.如果没有任何测试类编译为目标/测试类,请检查您的 pom 文件并确保包装不是“pom”。

Check pout if your current version of JUnit is still available in the Maven repository, otherwise, it would fail检查当前版本的 JUnit 是否在 Maven 存储库中仍然可用,否则会失败

The current version @time of this answer is此答案的当前版本@time 是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM