eclipse - 如何将 JUnit 5 与现有 Eclipse 项目集成?

标签 eclipse maven junit junit5

现有的 Eclipse 项目使用 Maven,但不了解 JUnit。我应该/可以将 JUnit 集成到现有项目中,还是应该创建一个专用于 JUnit 的新项目,或者是否有更好的选择?

最佳答案

您可以通过在 pom.xml 中包含以下依赖项来将 JUnit5 添加到该项目:

<properties>
    <junit.jupiter.version>5.0.1</junit.jupiter.version>
    <junit.platform.version>1.0.1</junit.platform.version> 
</properties>

<!--
    JUnit5 dependencies:
     * junit-jupiter-api: for writing JUnit5 tests
     * junit-jupiter-engine: for running JUnit5 tests
     * junit-platform-xxx: the foundation for JUnit5
     * (Optionally) you might want to include junit-vintage-engine for running JUnit4 tests       
-->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>${junit.jupiter.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>${junit.platform.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>${junit.platform.version}</version>
    <scope>test</scope>
</dependency>

要使 Maven Surefire 能够运行 JUnit5 测试,只需在 pom.xml 中包含以下插件定义:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven.surefire.plugin.version}</version>
    <configuration>
        <excludes>
            <exclude>**/MongoPopulatorTool.java</exclude>
        </excludes>
    </configuration>
    <dependencies>
        <!-- integrates JUnit5 with surefire -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>${junit.platform.version}</version>
        </dependency>
        <!-- ensures that a JUnit5-aware test engine is available on the classpath when running Surefire -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
        </dependency>
    </dependencies>
</plugin> 

最后,要使 Eclipse 的测试运行程序能够运行 JUnit5 测试,您必须运行 Eclipse Oxygen.1a (4.7.1a) 版本(或更高版本),请查看 Eclipse docs .

关于eclipse - 如何将 JUnit 5 与现有 Eclipse 项目集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49039215/

相关文章:

java - 在 Eclipse 控制台中显示 Grizzly 异常

java - 出生报告文件路径

eclipse - 使用eclipse在tomcat中启动/停止单个网络应用程序

java - JUnit中测试多线程与Java中Main函数的区别

java - 如何从 JUnit 测试访问 webapp 下的静态资源?

java - 在 eclipse/junit 中使用int ANT Build运行build.xml时出错

linux - Jenkins 中的 Maven 构建失败 : mvn: command not found

java - 使用 IntelliJ IDE 设置 Maven '-Denvironment'

maven - 无法使用 karma、jasmine、phantomjs 和 maven 加载 angular.js 进行单元测试

java - 在 JUnit 中测试 CharSequences 是否相等