我有一个多模块项目。每个项目都有自己的单元测试,用于父项目。当我使用mvn test时,不运行任何测试,目标文件夹不包含任何测试类。父项目甚至不创建目标文件夹
结构如下:
|-module1-> pom.xml
|-module2-> pom.xml
|-module3-> pom.xml
|-src/main
|-src/test/java/MyTest.java
|-pom.xml
见下面的poms(我省略了标准的pom样板)
父pom.xml
<groupId>com.tests</groupId>
<artifactId>unit-tests</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>RELEASE</version>
</dependency>
<dependencies>
Child pom:
<parent>
<groupId>com.tests</groupId>
<artifactId>unit-tests</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
每当我运行mvn clean test
时,都没有运行测试,而在控制台中我会得到类似的结果:
[INFO] ------------------------------------------------------------------------
[INFO] Building unit-tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
顺便说一句,父项目没有任何源代码,只是测试
答案 0 :(得分:3)
您正在使用包装pom
。使用这种包装,您只能运行绑定到阶段的少量目标(例如install
和deploy
)。为了在父母中运行测试,您必须明确表达您的意图,如下所示:
$mvn clean compiler:testCompile surefire:test
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tests.unit_tests.TestModules
Test1!
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.016 s <<< FAILURE! - in com.tests.unit_tests.TestModules
[ERROR] test1(com.tests.unit_tests.TestModules) Time elapsed: 0.003 s <<< FAILURE!
java.lang.AssertionError
at com.tests.unit_tests.TestModules.test1(TestModules.java:11)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TestModules.test1:11
[INFO]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] unit-tests ......................................... FAILURE [ 1.262 s]
[INFO] module1 ............................................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.473 s
[INFO] Finished at: 2017-12-02T11:04:50-02:00
[INFO] Final Memory: 16M/207M
[INFO] ------------------------------------------------------------------------
通过这种方式,您告诉maven
您要编译(生成目标)并在聚合器(具有父pom)中运行测试。否则maven
将仅运行模块中的测试。
$mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] unit-tests
[INFO] module1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building unit-tests 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building module1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ module1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ module1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ module1 ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ module1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ module1 ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] unit-tests ......................................... SUCCESS [ 0.002 s]
[INFO] module1 ............................................ SUCCESS [ 0.424 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.504 s
[INFO] Finished at: 2017-12-02T11:06:24-02:00
[INFO] Final Memory: 9M/241M
[INFO] ------------------------------------------------------------------------
我知道这是一个难读,但你应该检查一下: http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Packaging
例如,一个纯粹元数据的项目(包装价值是 pom)只将目标绑定到安装和部署阶段(完整的 某些包装类型的目标到构建阶段绑定列表, 请参阅Lifecycle Reference)。
修改强>
正如eis指出的那样。虽然有可能这样做,但maven很难做到,因为你应该不默认情况下这样做。您的单元测试应该在他们正在测试的模块中。分散它们不是一个好习惯。
答案是:你可以做到,是的。但你不应该!生活中有些事你可以做,但不应该......
如果我们谈论integration tests
这将是一个不同的故事..这是一个不同的讨论。
干杯!
答案 1 :(得分:1)
maven surefire插件运行所有JUnit Test,其中测试类的名称以 Test 结尾 - 使用大写&#39; T&#39;。根据您的信息,您的测试类名为Mytest
。重构要命名为MyTest
的类将使其有效。
答案 2 :(得分:0)
multimodule parent包含pom
,因此,根据定义,它只能包含pom,而不是任何源代码(甚至不是测试源代码!)。这就是您的测试无法运行并且您没有获得目标文件夹的原因:在pom类型的项目上无法运行或编译任何内容。多模块父模块仅用于将模块组合在一起。