JUnit5 Maven 依赖项

了解使用 Maven 配置其 JUnit5 的不同模块,以及如何使用它们创建和执行测试。

请注意,JUnit5 在运行时需要 Java8。

1. JUnit5 Maven 依赖项

要通过 maven 运行 JUnit5 测试,您将至少需要两个依赖项。

JUnit Jupiter 引擎依赖项

JUnit Jupiter 需要具有两个依赖项,即junit-jupiter-api和junit-jupiter-engine。junit-jupiter-api具有 junit 注解(例如@Test)以编写测试和扩展名,junit-jupiter-engine具有测试引擎实现,在运行时需要执行该引擎才能执行测试。在内部,junit-jupiter-engine依赖于junit-jupiter-api,因此添加junit-jupiter-engine即将两个依赖项都带入类路径。

<dependency>
	<groupId>org.junit.jupiter</groupId>
	<artifactId>junit-jupiter-engine</artifactId>
	<version>5.5.2</version>
</dependency>

我们来看看依赖树:

在这里插入图片描述
JUnit5 jupiter 引擎依赖树

JUnit 平台运行器依赖项

在 JUnit4 环境中,需要junit-platform-runner用于在 JUnit 平台上执行测试和测试套件。在内部,junit-platform-runner依赖于junit-platform-suite-api和junit-platform-launcher,因此添加junit-platform-runner仅将所有三个依赖项引入类路径中。

<dependency>
	<groupId>org.junit.platform</groupId>
	<artifactId>junit-platform-runner</artifactId>
	<version>${junit.platform.version}</version>
	<scope>test</scope>
</dependency>

我们来看看依赖树:

在这里插入图片描述
JUnit5 平台运行器依赖树

2. 使用 JUnit5 执行 JUnit4 测试

要在 JUnit5 环境中执行 JUnit4 测试,您将需要JUnit Platform Surefire Provider插件。只要您在 JUnit4 上配置test依赖项并将 JUnit Vintage TestEngine 实现添加到maven-surefire-plugin的依赖项中,它就可以运行基于 JUnit4 的测试,如下所示。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.0-M4</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>4.12.0-M4</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

通过在pom.xml中进行上述配置,现在您可以使用 JUnit5 运行旧的测试。

3. JUnit5 Maven 示例

用于运行用 JUnit5 构建的测试的示例pom.xml文件如下:

<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>com.howtodoinjava</groupId>
	<artifactId>JUnit5Examples</artifactId>

	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
		<junit.jupiter.version>5.5.2</junit.jupiter.version>
		<junit.platform.version>1.5.2</junit.platform.version>
	</properties>

	<dependencies>
		<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-runner</artifactId>
			<version>${junit.platform.version}</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.2</version>
			</plugin>
		</plugins>
	</build>
</project>
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值