断言AssertJ使用方式

Posted hct118

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了断言AssertJ使用方式相关的知识,希望对你有一定的参考价值。

 1 // entry point for all assertThat methods and utility methods (e.g. entry)
 2 import static org.assertj.core.api.Assertions.*;
 3 
 4 // basic assertions
 5 assertThat(frodo.getName()).isEqualTo("Frodo");
 6 assertThat(frodo).isNotEqualTo(sauron);
 7 
 8 // chaining string specific assertions
 9 assertThat(frodo.getName()).startsWith("Fro")
10                            .endsWith("do")
11                            .isEqualToIgnoringCase("frodo");
12 
13 // collection specific assertions (there are plenty more)
14 // in the examples below fellowshipOfTheRing is a List<TolkienCharacter>
15 assertThat(fellowshipOfTheRing).hasSize(9)
16                                .contains(frodo, sam)
17                                .doesNotContain(sauron);
18 
19 // as() is used to describe the test and will be shown before the error message
20 assertThat(frodo.getAge()).as("check %s‘s age", frodo.getName()).isEqualTo(33);
21 
22 // Java 8 exception assertion, standard style ...
23 assertThatThrownBy(() -> { throw new Exception("boom!"); }).hasMessage("boom!");
24 // ... or BDD style
25 Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
26 assertThat(thrown).hasMessageContaining("boom");
27 
28 // using the ‘extracting‘ feature to check fellowshipOfTheRing character‘s names (Java 7)
29 assertThat(fellowshipOfTheRing).extracting("name")
30                                .contains("Boromir", "Gandalf", "Frodo", "Legolas")
31 // same thing using a Java 8 method reference
32 assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName)
33                                .doesNotContain("Sauron", "Elrond");
34 
35 // extracting multiple values at once grouped in tuples (Java 7)
36 assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
37                                .contains(tuple("Boromir", 37, "Man"),
38                                          tuple("Sam", 38, "Hobbit"),
39                                          tuple("Legolas", 1000, "Elf"));
40 
41 // filtering a collection before asserting in Java 7 ... 
42 assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT)
43                                .containsOnly(sam, frodo, pippin, merry);
44 // ... or in Java 8
45 assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
46                                .containsOnly(aragorn, frodo, legolas, boromir);
47 
48 // combining filtering and extraction (yes we can)
49 assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
50                                .containsOnly(aragorn, frodo, legolas, boromir)
51                                .extracting(character -> character.getRace().getName())
52                                .contains("Hobbit", "Elf", "Man");

文档地址:http://joel-costigliola.github.io/assertj/index.html

以上是关于断言AssertJ使用方式的主要内容,如果未能解决你的问题,请参考以下文章

流式断言器AssertJ介绍

单元测试断言利器 AssertJ

在java测试中强制执行assertj

验证是不是在 Assertj 中调用了断言

走进Java接口测试之流式断言库AssertJ

postman添加断言