JUnit 5简明教程

概述

写测试用例对于开发来说有2点好处,一是开发阶段写完的功能可以快速验证,第二就是在后期需求变动或修改BUG后可以快速测试当前改动是否带来其它问题。下面就了解一下Junit5写测试用例。

准备

创建一个maven项目

mkdir junit5-tutorial
cd junit5-tutorial

mkdir -p src/main/java
mkdir -p src/test/java

mkdir -p src/main/resources
mkdir -p src/test/resources

# 编写pom.xml
vi pom.xml

添加依赖

  • 引入第三方断言库assertj
  • 支持json测试
  • 支持xml测试

pom.xml




    4.0.0

    com.example.xxx
    junit5-tutorial
    1.0-SNAPSHOT

    junit5-tutorial

    https://www.xxx.com

    
        UTF-8
        17
        17
    

    
        
            
                
                
                
                    maven-surefire-plugin
                    3.0.0-M6
                
                
                
                    maven-compiler-plugin
                    3.10.1
                
            
        

    

    
        
            
            
                net.bytebuddy
                byte-buddy
                1.12.10
            
            
                net.bytebuddy
                byte-buddy-agent
                1.12.10
                test
            

            
            
                org.mockito
                mockito-junit-jupiter
                4.5.1
                test
            
        
    

    

        
            org.assertj
            assertj-core
            3.22.0
            test
        
        
            io.github.classgraph
            classgraph
            4.8.146
        
        
            org.junit.jupiter
            junit-jupiter
            5.8.2
        

        
        
            net.javacrumbs.json-unit
            json-unit-assertj
            2.33.0
            test
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.13.2.2
        
        
            com.fasterxml.jackson.datatype
            jackson-datatype-jsr310
            2.13.2
        

        
        
            org.xmlunit
            xmlunit-assertj
            2.9.0
            test
        

    

创建一个User

public record User(String name, Integer age, Boolean blocked, LocalDate birthDate) {
}

测试

测试用例命名最佳实践

首先测试类名应该以Test结尾,测试用例名称最好遵从以下规则

  1. 测试名称应表达特定要求
  2. 测试名称应包含预期的输入或预期的结果
  3. 测试名称应以陈述的形式

具体参考:https://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html

断言

@Test
@DisplayName("User should be at least 18")
void user_should_be_at_least_18() {
    // junit5 的断言
    assertTrue(user.age() >= 18);
    // assertj 的断言
    assertThat(user.age()).isGreaterThanOrEqualTo(18);
}

显示名称

测试类和测试方法可以声明自定义显示名称,可以使用空格、特殊字符、甚至emojis表情符号,这些名称会在runner和测试报告中显示。
JUnit 5简明教程_第1张图片

参数化测试

参数化测试可以用不同的参数多次运行测试。它们和普通的@Test方法一样声明,但是使用@ParameterizedTest注解。还必须声明至少一个将为每次调用提供参数的来

使用@ValueSource来指定参数来源

它可以指定一个原生类型的数组,并且只能为每次调用提供一个参数

@ParameterizedTest
@ValueSource(ints = {20, 50, 80})
void test_value_source(int age) {
    assertThat(age).isGreaterThanOrEqualTo(18);
}

读取CSV文件内容作为参数来源

它可以让你使用classpath中的csv文件。csv文件中的每一行都会导致参数测试的一次调用

src/test/resources/friends.csv

name,age
lisa,20
hans,30
hanna,40
@ParameterizedTest
@CsvFileSource(resources = "/friends.csv", numLinesToSkip = 1)
void test_value_source_by_csv_file_source(String name, int age) {
    assertThat(age).isGreaterThanOrEqualTo(18);
}

标签

我们可以给测试类或测试用例上面通过@Tag加标签,执行测试的时候可以指定标签,从而达到为测试用例分组的目的。
下面就给测试类打上一个integration的标签

@Tag("integration")
class User01Test {
    // ...
}

可以使用如下命令来指定要执行的测试用例:

mvn test -Dgroups="integration"

左侧只执行了Running com.example.xxx.User01Test一个测试类,右侧则执行了3个
JUnit 5简明教程_第2张图片

总结

本文介绍了如何使用Junit5写测试用例。

参考

  1. Unit test naming best practices Unit test naming best practices - Stack Overflow
  2. https://osherove.com/blog/2005/4/3/naming-standards-for-unit-tests.html
  3. AssertJ Homepage https://assertj.github.io/doc/
  4. Gradle: https://stackoverflow.com/a/64986861
  5. https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions
  6. further reading
  7. JUnit 5 User Guide
  8. JUnit 5 中文文档 https://doczhcn.gitbook.io/junit5/
  9. AssertJ - fluent assertions java library
  10. Jupiter / JUnit 5 - Testcontainers
  11. awaitility/awaitility: Awaitility is a small Java DSL for synchronizing asynchronous operations (github.com)

你可能感兴趣的:(javajunit5)