JUnit入门

释放双眼,带上耳机,听听看~!

一、简介

与以前版本的JUnit不同,JUnit 5由来自三个不同子项目的几个不同模块组成。


1
2
3
1JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
2
3
  • JUnit Platform是基于JVM的运行测试的基础框架在,它定义了开发运行在这个测试框架上的TestEngine API。此外该平台提供了一个控制台启动器,可以从命令行启动平台,可以为Gradle和 Maven构建插件,同时提供基于JUnit 4的Runner。
  • JUnit Jupiter是在JUnit 5中编写测试和扩展的新编程模型和扩展模型的组合.Jupiter子项目提供了一个TestEngine在平台上运行基于Jupiter的测试。
  • JUnit Vintage提供了一个TestEngine在平台上运行基于JUnit 3和JUnit 4的测试。

需要java8或更高版本的jdk

二、环境

2.1 Eclipse

主流IDE都支持(IntelliJ IDEA,Eclipse, NetBeans和Visual Studio Code),可用(Gradle,Maven和 Ant)等工具轻松支持JUnit平台。

由于我使用都是Eclipse IDE for Java Developers。Version: 2018-12 (4.10.0)。所以没法全都讲。

Eclipse自带了。如图所示
JUnit入门

2.2 Maven

如果用Maven,一个完整的pom.xml如下


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4   <modelVersion>4.0.0</modelVersion>
5
6   <groupId>com.example</groupId>
7   <artifactId>junit5-jupiter-starter-maven</artifactId>
8   <version>1.0-SNAPSHOT</version>
9
10  <properties>
11      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12      <maven.compiler.source>1.8</maven.compiler.source>
13      <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
14      <junit.jupiter.version>5.5.2</junit.jupiter.version>
15  </properties>
16
17  <dependencies>
18      <dependency>
19          <groupId>org.junit.jupiter</groupId>
20          <artifactId>junit-jupiter</artifactId>
21          <version>${junit.jupiter.version}</version>
22          <scope>test</scope>
23      </dependency>
24  </dependencies>
25
26  <build>
27      <plugins>
28          <plugin>
29              <artifactId>maven-compiler-plugin</artifactId>
30              <version>3.8.1</version>
31          </plugin>
32          <plugin>
33              <artifactId>maven-surefire-plugin</artifactId>
34              <version>2.22.2</version>
35          </plugin>
36      </plugins>
37  </build>
38
39</project>
40
41

三、Hello JUnit

本文代码详见github仓库:https://github.com/landbroken/JavaLearning/tree/master/com.lyl.JUnit

待测试类


1
2
3
4
5
6
7
8
9
10
11
12
13
1package com.lyl.JUnit;
2
3public class Calculate {
4   public int add(int a,int b){
5       return a+b;
6   }
7  
8   public int minus(int a , int b){
9       return a-b;
10  }
11}
12
13

测试类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1package com.lyl.JUnit;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import org.junit.jupiter.api.Test;
5
6/**
7 * 第一个测试用例
8 * @author lyl
9 *
10 */
11public class FirstTests {
12  private final Calculate cal = new Calculate();
13  //@Test:表示方法是一种测试方法。
14  //与JUnit 4的@Test注释不同,此注释不会声明任何属性,因为JUnit Jupiter中的测试扩展基于它们自己的专用注释进行操作。
15  //除非被覆盖,否则这些方法是继承的。
16  @Test
17  public void testAdd() {
18      int result = cal.add(3, 3);
19      assertEquals(6,result);
20  }
21 
22  @Test
23  public void testMinus() {
24      int result = cal.minus(5, 2);
25      assertEquals(3,result);
26  }
27}
28
29
30

运行方法:右键-Run As/Coverage As-JUnit Test
运行结果:
JUnit入门
如果出现NoClassDefFoundError问题,可能是之前包没添加对。重新添加一下,或见参考文献[4]。

四、编写测试

4.1 注释

@Test

表示方法是一种测试方法。与JUnit 4的@Test注释不同,此注释不会声明任何属性,因为JUnit Jupiter中的测试扩展基于它们自己的专用注释进行操作。除非被覆盖,否则这些方法是继承的。

@ParameterizedTest

表示方法是参数化测试。除非被覆盖,否则这些方法是继承的。

@RepeatedTest

表示方法是重复测试的测试模板。除非被覆盖,否则这些方法是继承的。

@TestFactory

表示方法是动态测试的测试工厂。除非被覆盖,否则这些方法是继承的。

@TestTemplate

表示方法是设计为多次调用的测试用例的模板,具体取决于注册提供程序返回的调用上下文的数量。除非被覆盖,否则这些方法是继承的。

@TestMethodOrder

用于配置带注释的测试类的测试方法执行顺序 ; 类似于JUnit 4的@FixMethodOrder。这样的注释是继承的。

@TestInstance

用于为带注释的测试类配置测试实例生命周期。这样的注释是继承的。

@DisplayName

声明测试类或测试方法的自定义显示名称。这样的注释不是继承的。

@DisplayNameGeneration

声明测试类的自定义显示名称生成器。这样的注释是继承的。

@BeforeEach

表示该注释的方法应该被执行之前 的每个 @Test,@RepeatedTest,@ParameterizedTest,或@TestFactory方法在当前类; 类似于JUnit 4的@Before。除非被覆盖,否则这些方法是继承的。

@AfterEach

表示该注释的方法应该被执行之后 每个 @Test,@RepeatedTest,@ParameterizedTest,或@TestFactory方法在当前类; 类似于JUnit 4的@After。除非被覆盖,否则这些方法是继承的。

@BeforeAll

表示该注释的方法应该被执行之前 所有 @Test,@RepeatedTest,@ParameterizedTest,和@TestFactory方法在当前类; 类似于JUnit 4的@BeforeClass。这些方法是继承的(除非它们被隐藏或覆盖)并且必须是static(除非使用“每类” 测试实例生命周期)。

@AfterAll

表示该注释的方法应该被执行之后 的所有 @Test,@RepeatedTest,@ParameterizedTest,和@TestFactory方法在当前类; 类似于JUnit 4的@AfterClass。这些方法是继承的(除非它们被隐藏或覆盖)并且必须是static(除非使用“每类” 测试实例生命周期)。

@Nested

表示带注释的类是非静态嵌套测试类。除非使用“每类” 测试实例生命周期,否则不能在测试类中直接使用@BeforeAll和@AfterAll方法。这样的注释不是继承的。@Nested

@Tag

用于在类或方法级别声明用于过滤测试的标记 ; 类似于TestNG中的测试组或JUnit 4中的类别。此类注释在类级别继承,但不在方法级别继承。

@Disabled

用于禁用测试类或测试方法; 类似于JUnit 4的@Ignore。这样的注释不是继承的。

@ExtendWith

用于以声明方式注册扩展。这样的注释是继承的。

@RegisterExtension

用于通过字段以编程方式注册扩展。除非它们被遮蔽,否则这些字段是继承的。

@TempDir

用于通过生命周期方法或测试方法中的字段注入或参数注入来提供临时目录 ; 位于org.junit.jupiter.api.io包装中。

给TA打赏
共{{data.count}}人
人已打赏
安全技术

Bootstrap 间隔 (Spacing)

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索