maven没有编译测试代码/没有运行测试代码

时间:2018-03-25 11:18:08

标签: maven

我有一个简单的Mavenized Java项目,expected directory structure没有"业务"课程:

enter image description here

pom.xml相当平凡。它声明了对JUnit4和JUnit5的依赖关系,以及" surefire"插件(封闭project噪音消除):

<properties>
    <junit5Version>5.1.0</junit5Version>
    <junit4Version>4.12</junit4Version>   
    <commonsCodecVersion>20041127.091804</commonsCodecVersion>
    <mavenCompilerPluginVersion>3.7.0</mavenCompilerPluginVersion>
    <junitPlatformSurefireProviderVersion>1.1.0</junitPlatformSurefireProviderVersion>
    <mavenSurefirePluginVersion>2.21.0</mavenSurefirePluginVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${mavenCompilerPluginVersion}</version>
            <configuration>
                <release>9</release>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${mavenSurefirePluginVersion}</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junitPlatformSurefireProviderVersion}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>${junit5Version}</version>
                </dependency>

                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit5Version}</version>

                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<!-- https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-maven/ -->

<dependencies>

    <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec/ -->
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>${commonsCodecVersion}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit4Version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit5Version}</version>
    </dependency>

</dependencies>

问题:

  1. 在上文中,我并未考虑junit:junitorg.junit.jupiter.junit-jupiter-api的范围 对test的{​​{1}}依赖关系 范围。实际上,如果我这样做,mvn compile会失败,因为它不能 找到JUnit4和JUnit5的符号。这是意料之外的,我愿意 期望它根本不会实际编译测试代码。确实 示例通常显示对test范围的限制。什么&#39; S 错误?
  2. 如果对test范围没有限制,则mvn compile有效,但mvn test不会运行任何测试。 surefire插件似乎没有嗅出任何JUnit测试,无论是4还是5。这里有什么问题?
  3. 最后,版本信息:

    $ mvn -version
    Apache Maven 3.5.2
    Maven home: /usr/local/java/maven
    Java version: 9.0.4, vendor: Oracle Corporation
    

1 个答案:

答案 0 :(得分:1)

maven-surefire-plugin 2.21.0与junit-platform-surefire-provider版本1.1.0不兼容。

maven-surefire-plugin回滚到2.19.1或将junit-platform-surefire-provider更新为1.2.0-SNAPSHOT(如果已经推出则更新为1.2.0版本)。

使用SNAPSHOT时要使用的pom.xml,并配置了Sonatype插件存储库:

<?xml version="1.0"?>
<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>TestJava</groupId>
  <artifactId>TestJava</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <pluginRepositories>
    <pluginRepository>
      <id>oss-sonatype</id>
      <name>oss-sonatype</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </pluginRepository>
  </pluginRepositories>
  <properties>
    <junit5Version>5.1.0</junit5Version>
    <junit4Version>4.12</junit4Version>
    <mavenCompilerPluginVersion>3.7.0</mavenCompilerPluginVersion>
    <junitPlatformSurefireProviderVersion>1.2.0-SNAPSHOT</junitPlatformSurefireProviderVersion>
    <mavenSurefirePluginVersion>2.21.0</mavenSurefirePluginVersion>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${mavenCompilerPluginVersion}</version>
        <configuration>
          <release>9</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${mavenSurefirePluginVersion}</version>
        <!-- https://www.petrikainulainen.net/programming/testing/junit-5-tutorial-running-unit-tests-with-maven/ -->
        <dependencies>
          <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>${junitPlatformSurefireProviderVersion}</version>
          </dependency>
          <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit5Version}</version>
          </dependency>
          <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit5Version}</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit4Version}</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${junit5Version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

运行mvn -U test以强制检查远程存储库中缺少的版本和更新的快照。