当前位置: 首页 > 工具软件 > AssertJ > 使用案例 >

assertJ断言

颛孙天宇
2023-12-01

junit.jupiter 包里的断言

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;

assertj

推荐使用这个,更好用,非常强大

AssertJ - fluent assertions java library 官网

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.within;
​
import static org.assertj.core.api.Assertions.*;  //放入基类引用

举例 String,Object ,list,int,

// entry point for all assertThat methods and utility methods (e.g. entry)
import static org.assertj.core.api.Assertions.*;
​
// basic assertions 基本断言
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron);
​
// chaining string specific assertions  字符串链式断言
assertThat(frodo.getName()).startsWith("Fro")
                           .endsWith("do")
                           .isEqualToIgnoringCase("frodo");
​
// collection specific assertions (there are plenty more)
// in the examples below fellowshipOfTheRing is a List<TolkienCharacter>
assertThat(fellowshipOfTheRing).hasSize(9)
                               .contains(frodo, sam)
                               .doesNotContain(sauron);
​
// as() is used to describe the test and will be shown before the error message  会在错误前面写chek xx's age
assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);
​
// exception assertion, standard style ... 异常断言
assertThatThrownBy(() -> { throw new Exception("boom!"); }).hasMessage("boom!");
// ... or BDD style
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
assertThat(thrown).hasMessageContaining("boom");
​
// using the 'extracting' feature to check fellowshipOfTheRing character's names   取对象里的名字判断
assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName)
                               .doesNotContain("Sauron", "Elrond");
​
// extracting multiple values at once grouped in tuples 取多个值判断
assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
                               .contains(tuple("Boromir", 37, "Man"),
                                         tuple("Sam", 38, "Hobbit"),
                                         tuple("Legolas", 1000, "Elf"));
​
// filtering a collection before asserting  过滤后断言
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
                               .containsOnly(aragorn, frodo, legolas, boromir);
​
// combining filtering and extraction (yes we can)  过滤加提取后断言
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
                               .containsOnly(aragorn, frodo, legolas, boromir)
                               .extracting(character -> character.getRace().getName())
                               .contains("Hobbit", "Elf", "Man");
​
// and many more assertions: iterable, stream, array, map, dates, path, file, numbers, predicate, optional ...

 类似资料: