【问题标题】:pom.xml Maven configuration to run JUnit 4 + JUnit 5 Jupiter tests in mixed Java + Kotlin projectpom.xml Maven 配置以在混合 Java + Kotlin 项目中运行 JUnit 4 + JUnit 5 Jupiter 测试
【发布时间】:2019-05-25 16:44:47
【问题描述】:

我无法成功配置 maven 项目以运行 Java 和 Kotlin 的混合 JUnit 4 / JUnit 5 Jupiter 测试。

配置 Gradle 没有问题(例如 this configuration works well 满足我的需要),但不是 maven。我尝试了不同配置的 maven、Kotlin、surefire 插件以及不同版本的 JUnit Jupiter 和平台依赖项,但没有成功......

你们中的任何人都可以提供工作示例吗?

我还在 JUnit 团队 GitHub 问题和 Kotlin slack 频道中发布了类似的问题: - https://github.com/junit-team/junit5/issues/1899 - https://kotlinlang.slack.com/archives/C0922A726/p155880442804330


问候, 马克西姆

【问题讨论】:

  • 您应该配置 Maven 项目以使用可以运行(通过老式)JUnit 4 测试的 JUnit Jupiter。 (或多或少与 Gradle 相同)。
  • 嘿@khmarbaise,你能在这里发布你正在谈论的工作 pom.xml 示例的链接吗?谢谢!

标签: java maven kotlin junit maven-surefire-plugin


【解决方案1】:

好的...我花了几个小时似乎找到了答案。这里是多模块 maven 项目的工作 pom.xml 文件示例,用于执行混合 JUnit 4 Vintage 和 JUnit 5 Jupiter 测试以及混合 Java 和 Kotlin 测试类,限制如下:

src/
  test/
    java/
      **/*JUnit4VintageJavaTest.java  +
      **/*JUnit4VintageKotlinTest.kt  +
      **/*JUnit5JupiterJavaTest.java  +
      **/*JUnit5JupiterKotlinTest.kt  +
    kotlin/
      **/*JUnit4VintageJavaTest.java  -
      **/*JUnit4VintageKotlinTest.kt  +
      **/*JUnit5JupiterJavaTest.java  -
      **/*JUnit5JupiterKotlinTest.kt  +

在哪里

+ 表示:将要执行测试

- 表示:由于某些原因,测试不会使用提供的配置执行...

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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.github.daggerok</groupId>
  <artifactId>mixed-kotlin-java-jupiter-tests-maven-execution</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <!--
  <modules>
    <module>module-1</module>
    <module>module-2</module>
  </modules>
  -->

  <properties>
    <java.version>1.8</java.version>
    <kotlin.version>1.3.31</kotlin.version>
    <junit-jupiter.version>5.5.0-M1</junit-jupiter.version>
    <junit-platform.version>1.5.0-M1</junit-platform.version>

    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>3.0.0-M3</maven-surefire-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-stdlib</artifactId>
      <version>${kotlin.version}</version>
    </dependency>

    <!-- junit 4 -->
    <dependency><!-- already contains junit:4.12 dependency, so we are not going to add it explicitly! -->
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-test-junit</artifactId>
      <version>${kotlin.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency><!-- vintage engine is required if you wanna execute JUnit 4 together with JUnit 5 Jupiter tests... -->
      <groupId>org.junit.vintage</groupId>
      <artifactId>junit-vintage-engine</artifactId>
      <version>${junit-jupiter.version}</version>
      <scope>test</scope>
    </dependency>

    <!-- junit 5 -->
    <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-test-junit5</artifactId>
      <version>${kotlin.version}</version>
      <scope>test</scope>
      <!-- includes junit-jupiter-api:5.0.0 and junit-jupiter-engine:5.2.0, but we need other versions -->
        <exclusions>
            <exclusion>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency><!-- includes junit-jupiter-api and junit-jupiter-engine dependencies -->
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>${junit-jupiter.version}</version>
        <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <defaultGoal>test</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-maven-plugin</artifactId>
        <version>${kotlin.version}</version>
        <configuration>
          <experimentalCoroutines>enable</experimentalCoroutines>
        </configuration>
        <executions>
          <execution>
            <id>compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <sourceDirs>
                <sourceDir>${project.basedir}/src/main/java</sourceDir>
                <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
              </sourceDirs>
            </configuration>
          </execution>
          <execution>
            <id>test-compile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>test-compile</goal>
            </goals>
            <configuration>
              <sourceDirs>
                <sourceDir>${project.basedir}/src/test/java</sourceDir>
                <sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
              </sourceDirs>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>none</phase>
          </execution>
          <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>${maven-surefire-plugin.version}</version>
      </plugin>
    </plugins>
  </build>

</project>

工作示例位于here

如何验证:

git clone https://github.com/daggerok/mixed-kotlin-java-jupiter-tests testme ; cd $_ ; mvn test

唯一的问题无法回答是否可能以及如何在允许的位置配置 maven 项目并识别 src/test/kotlin 测试源目录中的 java 测试类?但似乎这是一个不同的问题,必须关闭。

我的 PR 和相应的示例刚刚合并到 junit-team/junit5-samples 项目并且可以使用 here

【讨论】:

  • 很有可能kotlin源目录中的*Test.java测试没有被执行,因为它们一开始没有被编译,因为maven java编译器插件不知道它应该看起来java源在 kotlin 目录中。
  • @Ilya,看起来像这样。问题是:是否可以正确配置 Maven 来做需要的事情? Gradle 可以从两个目录成功运行两种语言的测试...
猜你喜欢
  • 2021-10-09
  • 2019-06-08
  • 2020-12-31
  • 2019-04-25
  • 2016-11-12
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多