java junit4 for_java – 在同一版本中执行JUnit 4和JUnit 5测试

本文介绍了如何在Maven项目中同时编译和执行JUnit 4和JUnit 5测试。解决方案包括使用JUnit Platform、JUnit Jupiter和JUnit Vintage,以及更新Maven Surefire插件至2.22.0或更高版本,以实现无缝集成。
摘要由CSDN通过智能技术生成

在Maven项目中,我有一些现有的测试依赖于JUnit 4.由于多种原因,我无法在JUnit 5中迁移这些测试.

实质上,一些测试依赖于使用JUnit 4 runner的库,代码迁移可能需要一些时间.

我想用JUnit 5创建新的测试类,现在已经发布并提供了新的有趣功能.

怎么做 ?

解决方法:

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

每个项目都是一个独特的项目,使用它们可以在同一个项目中编译和执行JUnit 4和JUnit 5测试.

JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5.

JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

The JUnit Platform serves as a foundation for launching testing frameworks on the JVM

更新:来自Maven Surefire 2.22.0

Starting with version 2.22.0, Maven Surefire provides native support

for executing tests on the JUnit Platform.

所以配置要简单得多.

请注意,junit-4 api依赖项是可选的,因为现在需要的引擎依赖项已经拉出了默认的api版本(junit 4和5都是这种情况).

这是一个示例pom.xml.

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">

4.0.0

david

jupiter-4-and-5-same-build

0.0.1-SNAPSHOT

1.8

1.8

5.1.0

4.12

org.junit.jupiter

junit-jupiter-engine

${junit-jupiter.version}

test

org.junit.vintage

junit-vintage-engine

${junit-jupiter.version}

junit

junit

${junit.version}

test

maven-surefire-plugin

2.22.0

旧方式:对于Maven Surefire低于2.22.0

以下是与Maven一起使用的最小配置,用于配置项目以编译和运行JUnit4和JUnit5测试:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

mygroup

minimal-conf-junit4-5

0.0.1-SNAPSHOT

1.8

1.8

4.12

4.12.1

5.0.1

1.0.1

org.junit.jupiter

junit-jupiter-api

${junit-jupiter.version}

test

junit

junit

${junit.version}

test

maven-surefire-plugin

2.19.1

org.junit.platform

junit-platform-surefire-provider

${junit-platform.version}

org.junit.vintage

junit-vintage-engine

${junit-vintage-engine}

org.junit.jupiter

junit-jupiter-engine

${junit-jupiter.version}

现在mvn test编译并运行JUnit 4和JUnit 5测试.

注1:junit-vintage-engine(4.12.1)和junit(4.12)依赖项没有指定相同的确切版本.

这根本不是问题:

>他们的释放与他们之间没有关系

> junit-vintage-engine旨在运行任何JUnit 3或4测试.

注2:带有2.19.1版本的maven-surefire-plugin对于编译JUnit 5测试类或JUnit 4和JUnit 5测试类都很重要.

下一版本的插件在JUnit 5测试执行期间确实会导致一些异常,但2.22.0最终解决了这个问题(参见答案的第一部分:“更新:来自Maven Surefire 2.22.0”).

标签:java,maven,junit5,junit4

来源: https://codeday.me/bug/20190930/1835754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值