Kotlin – Maven不执行测试

所以我有一个我想测试的Kotlin应用程序。 我的测试( .kt )文件,在Eclipse中成功执行。 (测试本身是一个h2 mock jdbc测试)。

现在当运行mvn test -X它说:

  releases: [enabled => true, update => never] ] [DEBUG] (s) reportFormat = brief [DEBUG] (s) reportsDirectory = C:\Users\Ivar\workspace\anotherworkspace\newrestservice\complete\target\surefire-reports [DEBUG] (f) rerunFailingTestsCount = 0 [DEBUG] (f) reuseForks = true [DEBUG] (s) runOrder = filesystem [DEBUG] (f) shutdown = testset [DEBUG] (s) skip = false [DEBUG] (f) skipAfterFailureCount = 0 [DEBUG] (s) skipTests = false [DEBUG] (s) suiteXmlFiles = [] [DEBUG] (s) testClassesDirectory = C:\Users\Ivar\workspace\anotherworkspace\newrestservice\complete\target\test-classes [DEBUG] (s) testFailureIgnore = false [DEBUG] (s) testNGArtifactName = org.testng:testng [DEBUG] (s) testSourceDirectory = C:\Users\Ivar\workspace\anotherworkspace\newrestservice\complete\src\test\java [DEBUG] (s) threadCountClasses = 0 [DEBUG] (s) threadCountMethods = 0 [DEBUG] (s) threadCountSuites = 0 [DEBUG] (s) trimStackTrace = true [DEBUG] (s) useFile = true [DEBUG] (s) useManifestOnlyJar = true [DEBUG] (s) useSystemClassLoader = true [DEBUG] (s) useUnlimitedThreads = false [DEBUG] (s) workingDirectory = C:\Users\Ivar\workspace\anotherworkspace\newrestservice\complete [DEBUG] (s) project = MavenProject: org.springframework:gs-rest-service:0.1.0 @ C:\Users\Ivar\workspace\anotherworkspace\newrestservice\complete\pom.xml 

它不执行任何测试(它不能find他们)

这是我的pom.xml

   4.0.0 org.springframework gs-rest-service 0.1.0 war  org.springframework.boot spring-boot-starter-parent 1.4.0.RELEASE    org.springframework.boot spring-boot-starter-web   org.springframework.boot spring-boot-starter-test test   org.springframework spring-jdbc  <!--  com.jayway.jsonpath json-path test -->  org.jetbrains.kotlin kotlin-stdlib 1.0.3   org.postgresql postgresql 9.4.1211   commons-dbutils commons-dbutils 1.6   com.h2database h2 1.4.191     1.8     org.springframework.boot spring-boot-maven-plugin   kotlin-maven-plugin org.jetbrains.kotlin 1.0.3   compile  compile    ${project.basedir}/src/main/java     test-compile  test-compile    ${project.basedir}/src/main/java       org.apache.maven.plugins maven-compiler-plugin 3.5.1    default-compile none    default-testCompile none   java-compile compile  compile    java-test-compile test-compile  testCompile      org.apache.maven.plugins maven-surefire-plugin 2.19.1   **/Test*.kt **/*Test.kt **/*TestCase.kt        spring-releases https://repo.spring.io/libs-release     spring-releases https://repo.spring.io/libs-release    

两个问题,你遇到的第一个问题 ,但我会在这里记录。 在Kotlin-Maven插件中,您将test-compile目标设置为:

  test-compile  test-compile    ${project.basedir}/src/main/java    

这是编译src/main/java目录,而不是src/test/java所以你的测试根本不被编译。 应该:

   ${project.basedir}/src/test/java  

或者,如果您的文件都在javakotlin目录中:

   ${project.basedir}/src/test/java ${project.basedir}/src/test/kotlin  

第二个问题可能是你试图解决第一个问题。 它看起来很好,但是Surefire插件的配置不正确。 Maven Surefire插件的文档不太准确(或者它不完整)。 如果你完全删除你的Surefire插件配置,它将工作,因为默认已经做了你想要的。 或者你可以改变从include文件模式从.kt后缀,而不是.class.java后缀,这些将工作正常,即使Kotlin,Clojure,斯卡拉,…

为什么? 这里是故事…

这个配置:

   org.apache.maven.plugins maven-surefire-plugin 2.19.1   **/Test*.java **/*Test.java **/*TestCase.java **/RandomName.java    

不做你认为的事情。 这实际上在Surefire中通过搜索替换.java替换为.class ,并在内部变成:

    **/Test*.class **/*Test.class **/*TestCase.class **/RandomName.class   

但是如果你使用.kt扩展名,你可以打破这个硬编码的魔法。 您可以在Surefire插件的源代码中看到它,它用.class替换以.java结尾的文件。 哎呀,它跟源文件根本没有关系,而且正在寻找编译类。

在Surefire插件2.19他们增加了具有正则expression式和完全限定的类名模式的能力 。 所以插件决定你使用的方式似乎是由文件扩展名.java 。 如果它看到.java它知道每个.java文件都变成了一个同名的类,所以它会查找匹配模式的类,而不是源代码。 源代码已经由编译器插件编译,而不是测试运行器。 它没有业务寻找源代码。

所以这个设置是误导性的,实际上是用“魔术”来找出一个类名,而不是只要求类名。 当然,样本和一切完全让你相信它与源文件有关。 事实并非如此。

最好不要使用配置,并遵循默认的命名约定。 或者,如果您必须指定某些内容,请使用include配置的更现代的正则expression式或类名称版本:

   org.apache.maven.plugins maven-surefire-plugin 2.19.1   Test* *Test *TestCase RandomName    

现在正在比较任何包中包含这些名称的类。