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

发表于:2021-8-09 09:38  作者:佚名   来源:网络

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签: 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);
  }
  }

     本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理

评 论

论坛新帖



建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海信义律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2022, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道