
在本文中,我们将向您展示如何使用AssertJ编写测试断言。
PS已通过JUnit 5.5.2和AssertJ 3.14.0测试
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.5.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.14.0</version> <scope>test</scope> </dependency> |
1.对AssertJ的JUnit 5断言
将JUnit 5断言转换为AssetJ很容易,请参见以下语法:
JUnit 5
1 2 3 | assertEquals(expected, actual); assertEquals(expected, actual, "assertion desc"); |
AssertJ
1 2 3 | assertThat(actual).isEqualTo(expected); assertThat(actual).as("assertion desc").isEqualTo(expected); |
2. AssertJ
2.1使用AssertJ的一些典型测试断言–
AssertjTest.java
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 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<String> 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<String, Object> 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<String, Object> 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<String> 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断言
标签: 断言 assertj 异常 junit 5