Java Junit5单元测试配置使用方法及示例代码

本文主要介绍Java中,使用Junit5进行单元测试的配置使用方法,以及相关的示例代码。

1、安装引用Junit5

JUnit5需要Gradle 4.6+Maven 2.22.0+的版本。

1) Maven中添加到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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>junit5-jupiter-starter-maven</artifactId>
	<version>1.0-SNAPSHOT</version>

	<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.6.2</junit.jupiter.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter</artifactId>
			<version>${junit.jupiter.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>

2) Gradle中添加到build.gradle

plugins {
	id 'java'
	id 'eclipse' // optional (to generate Eclipse project files)
	id 'idea' // optional (to generate IntelliJ IDEA project files)
}
repositories {
	mavenCentral()
}
dependencies {
	testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
test {
	useJUnitPlatform()
	testLogging {
		events "passed", "skipped", "failed"
	}
}

2、测试示例代码

1) 逻辑代码

/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */
package com.example.project;
public class Calculator {
	public int add(int a, int b) {
		return a + b;
	}
}

2) 测试代码

/*
 * Copyright 2015-2018 the original author or authors.
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v2.0 which
 * accompanies this distribution and is available at
 *
 * http://www.eclipse.org/legal/epl-v20.html
 */
package com.example.project;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class CalculatorTests {
	@Test
	@DisplayName("1 + 1 = 2")
	void addsTwoNumbers() {
		Calculator calculator = new Calculator();
		assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
	}
	@ParameterizedTest(name = "{0} + {1} = {2}")
	@CsvSource({
			"0,    1,   1",
			"1,    2,   3",
			"49,  51, 100",
			"1,  100, 101"
	})
	void add(int first, int second, int expectedResult) {
		Calculator calculator = new Calculator();
		assertEquals(expectedResult, calculator.add(first, second),
				() -> first + " + " + second + " should equal " + expectedResult);
	}
}

示例项目代码:

https://github.com/junit-team/junit5-samples/tree/r5.6.2/junit5-jupiter-starter-gradle/src

https://github.com/junit-team/junit5-samples/tree/r5.6.2/junit5-jupiter-starter-maven/src

相关文档:

https://junit.org/junit5/docs/current/user-guide/#overview-getting-started-example-projects


推荐阅读
cjavapy编程之路首页