JUnit 5 + AssertJ示例

JUnit 5 + AssertJ示例_第1张图片 JUnit 5 + AssertJ示例_第2张图片

在本文中,我们将向您展示如何使用AssertJ编写测试断言。

PS已通过JUnit 5.5.2和AssertJ 3.14.0测试

pom.xml

		org.junit.jupiter
		junit-jupiter-engine
		5.5.2
		test
	
		
	
		org.assertj
		assertj-core
		3.14.0
		test
	

1.对AssertJ的JUnit 5断言

将JUnit 5断言转换为AssetJ很容易,请参见以下语法:

JUnit 5
assertEquals(expected, actual);

assertEquals(expected, actual, "assertion desc");
AssertJ
assertThat(actual).isEqualTo(expected);

assertThat(actual).as("assertion desc").isEqualTo(expected);

2. AssertJ

2.1使用AssertJ的一些典型测试断言– StringListMapException

AssertjTest.java
package com.mkyong.assertions.assertj;

import org.assertj.core.api.InstanceOfAssertFactories;
import org.assertj.core.data.Index;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

public class AssertjTest {

    // assert string
    @Test
    void test_string_ok() {

        String name = "I am Mkyong!";

        assertThat(name)
                .as("if failed display this msg!")
                .isEqualTo("I am Mkyong!")
                .isEqualToIgnoringCase("I AM mkyong!")
                .startsWith("I")
                .endsWith("!")
                .containsIgnoringCase("mkyong");

    }

    // assert list
    @Test
    void test_list_ok() {

        List list = Arrays.asList("Java", "Rust", "Clojure");

        assertThat(list)
                .hasSize(3)
                .contains("Java", "Clojure")
                .contains("Java", Index.atIndex(0))
                .contains("Rust", Index.atIndex(1))
                .contains("Clojure", Index.atIndex(2))
                .doesNotContain("Node JS");

    }

    // assert map
    @Test
    void test_map_ok() {

        Map map = new HashMap<>();
        map.put("name", "mkyong");

        assertThat(map)
                .hasSize(1)
                .extractingByKey("name", as(InstanceOfAssertFactories.STRING))
                .isEqualToIgnoringCase("mkyong")
                .startsWith("mkyong");

        assertThat(map).extracting("name")
                .isEqualTo("mkyong");

        Map map2 = new HashMap<>();
        map2.put("number", 999);

        assertThat(map2)
                .hasSize(1)
                .extractingByKey("number", as(InstanceOfAssertFactories.INTEGER))
                .isEqualTo(999);

    }


    // assert exception
    @Test
    void test_exception_ok() {

        assertThatThrownBy(() -> divide(1, 0))
                .isInstanceOf(ArithmeticException.class)
                .hasMessageContaining("zero")
                .hasMessage("/ by zero");

        assertThatThrownBy(() -> {
            List list = Arrays.asList("one", "two");
            list.get(2);
        })
                .isInstanceOf(IndexOutOfBoundsException.class)
                .hasMessageContaining("Index 2 out of bounds");

    }

    int divide(int input, int divide) {
        return input / divide;
    }

}

下载源代码

$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-examples
$检查src / test / java / com / mkyong / assertions / assertj / *。java

参考文献

  • AssertJ –流利的断言Java库
  • 将您的JUnit 5断言转换为AssertJ
  • JUnit 5断言

翻译自: https://mkyong.com/junit5/junit-5-assertj-examples/

你可能感兴趣的:(java,git,rust)