断言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

转载于:https://www.cnblogs.com/HCT118/p/7865424.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值