2016-09-27 90 views
1

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

现在运行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 

而且它不执行任何测试(它无法找到EM)

这里是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>org.springframework</groupId> 
    <artifactId>gs-rest-service</artifactId> 
    <version>0.1.0</version> 
    <packaging>war</packaging> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.0.RELEASE</version> 
    </parent> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
     </dependency> 
     <!-- <dependency> 
      <groupId>com.jayway.jsonpath</groupId> 
      <artifactId>json-path</artifactId> 
      <scope>test</scope> 
     </dependency>--> 
     <dependency> 
      <groupId>org.jetbrains.kotlin</groupId> 
      <artifactId>kotlin-stdlib</artifactId> 
      <version>1.0.3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.postgresql</groupId> 
      <artifactId>postgresql</artifactId> 
      <version>9.4.1211</version><!--$NO-MVN-MAN-VER$ --> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbutils</groupId> 
      <artifactId>commons-dbutils</artifactId> 
      <version>1.6</version> 
     </dependency> 
     <dependency> 
      <groupId>com.h2database</groupId> 
      <artifactId>h2</artifactId> 
      <version>1.4.191</version> 
     </dependency> 
     <!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect --> 


    </dependencies> 

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 


    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
      <plugin> 
       <artifactId>kotlin-maven-plugin</artifactId> 
       <groupId>org.jetbrains.kotlin</groupId> 
       <version>1.0.3</version> 
       <executions> 
        <execution> 
         <id>compile</id> 
         <goals> 
          <goal>compile</goal> 
         </goals> 
         <configuration> 
          <sourceDirs> 
           <sourceDir>${project.basedir}/src/main/java</sourceDir> 
          </sourceDirs> 
         </configuration> 
        </execution> 
        <execution> 
         <id>test-compile</id> 
         <goals> 
          <goal>test-compile</goal> 
         </goals> 
         <configuration> 
          <sourceDirs> 
           <sourceDir>${project.basedir}/src/main/java</sourceDir> 
          </sourceDirs> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version><!--$NO-MVN-MAN-VER$ --> 
       <executions> 
        <!-- Replacing default-compile as it is treated specially by maven --> 
        <execution> 
         <id>default-compile</id> 
         <phase>none</phase> 
        </execution> 
        <!-- Replacing default-testCompile as it is treated specially by maven --> 
        <execution> 
         <id>default-testCompile</id> 
         <phase>none</phase> 
        </execution> 
        <execution> 
         <id>java-compile</id> 
         <phase>compile</phase> 
         <goals> 
          <goal>compile</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>java-test-compile</id> 
         <phase>test-compile</phase> 
         <goals> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>2.19.1</version><!--$NO-MVN-MAN-VER$--> 
        <configuration> 
         <includes> 
          <include>**/Test*.kt</include> 
          <include>**/*Test.kt</include> 
          <include>**/*TestCase.kt</include> 
         </includes> 
        </configuration> 
       </plugin> 
     </plugins> 
    </build> 

    <repositories> 
     <repository> 
      <id>spring-releases</id> 
      <url>https://repo.spring.io/libs-release</url> 
     </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-releases</id> 
      <url>https://repo.spring.io/libs-release</url> 
     </pluginRepository> 
    </pluginRepositories> 
</project> 
+0

Maven文档通常不太清楚设置,它们的用法以及它们的含义。您遇到了Maven文档不足的典型例子。我的答案显示了这个问题... –

回答

4

两个问题,第一个问题你发现自己,但我会在这里记录。在科特林,Maven插件你有test-compile目标为设置:

<execution> 
     <id>test-compile</id> 
     <goals> 
      <goal>test-compile</goal> 
     </goals> 
     <configuration> 
      <sourceDirs> 
       <sourceDir>${project.basedir}/src/main/java</sourceDir> 
      </sourceDirs> 
     </configuration> 
</execution> 

这是编译的,而不是src/test/javasrc/main/java目录,以便您的测试并不是在所有的编译。应该是:

  <sourceDirs> 
       <sourceDir>${project.basedir}/src/test/java</sourceDir> 
      </sourceDirs> 

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

  <sourceDirs> 
       <sourceDir>${project.basedir}/src/test/java</sourceDir> 
       <sourceDir>${project.basedir}/src/test/kotlin</sourceDir> 
      </sourceDirs> 

第二个问题可能你试图解决的第一个。它看起来很好,但是Surefire插件的配置不正确。 Maven Surefire插件的文档不太准确(或者它不完整)。如果你完全删除了Surefire插件配置,它将会工作,因为默认设置已经做到了你想要的。或者你也可以从具有.kt后缀改为.class.java后缀以及那些将罚款甚至科特林,Clojure中,斯卡拉,列出include文件模式的改变...

为什么?这里是故事...

此配置:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.19.1</version> 
      <configuration> 
       <includes> 
        <include>**/Test*.java</include> 
        <include>**/*Test.java</include> 
        <include>**/*TestCase.java</include> 
        <include>**/RandomName.java</include> 
       </includes> 
      </configuration> 
     </plugin> 

没有做什么,你认为它。这实际上是在神火做一个搜索和替换.java.class转化,并在内部变为:

  <configuration> 
       <includes> 
        <include>**/Test*.class</include> 
        <include>**/*Test.class</include> 
        <include>**/*TestCase.class</include> 
        <include>**/RandomName.class</include> 
       </includes> 
      </configuration> 

但是如果你使用.kt扩展你打破这个硬编码的魔力。您可以在source code of Surefire plugin中看到它,它将替换以.java结尾的文件和.classOoops,它根本与源文件无关,并且正在寻找编译类。

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

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

最好不要使用配置,并遵循默认的命名约定。或者,如果你必须指定的东西,用更现代的正则表达式或类名称版本的include配置:

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.19.1</version> 
      <configuration> 
       <includes> 
        <include>Test*</include> 
        <include>*Test</include> 
        <include>*TestCase</include> 
        <include>RandomName</include> 
       </includes> 
      </configuration> 
     </plugin> 

这是现在比较任一类别与任何包的名称。

+0

原来这不是问题,问题是测试没有编译(maven插件编译器'sourceDir')。你的(非常详细和解释性的)答案帮助我解决了这个问题,所以我会接受它! – Ivaro18

+0

@ Ivaro18如果编译包含'* .kt'实际上工作吗?我认为这仍然是失败的? –

+0

不确定,我在添加sourceDir之前将其更改为类名版本 – Ivaro18