官方地址:http://joel-costigliola.github.io/assertj/
开源地址:https://github.com/joel-costigliola/assertj-core
从官网上我们可以看到AssertJ支持的库有这些,具体用法上官方查询:
Core:AssertJ core is a Java library that provides a fluent interface for writing assertions.
Assertions generator:Use the Assertion Generator to create assertions specific to your own classes.
Guava:AssertJ assertions for Guava provides assertions for Guava types like Multimap, Table, Optional, Range or ByteSource.
Joda-Time:AssertJ assertions for Joda-Time provides assertions for Joda-Time types like DateTime and LocalDateTime.
DB:AssertJ-DB provides assertions to test data in a database.
Neo4j:Provides assertions for Neo4j 2 or higher.
Swing:AssertJ Swing is a Java library that provides a fluent interface for functional Swing UI testing.
maven引入依赖如下:
<dependency>
<groupId>org.assertjgroupId>
<artifactId>assertj-coreartifactId>
<version>1.7.1version>
<scope>testscope>
dependency>
此外对应的开发库引入如下:
<dependency>
<groupId>org.assertjgroupId>
<artifactId>assertj-joda-timeartifactId>
<version>1.1.0version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.assertjgroupId>
<artifactId>assertj-guavaartifactId>
<version>2.0.0version>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.assertjgroupId>
<artifactId>assertj-coreartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.assertjgroupId>
<artifactId>assertj-neo4jartifactId>
<version>1.0.0version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.assertjgroupId>
<artifactId>assertj-dbartifactId>
<version>1.0.1version>
<scope>testscope>
dependency>
官方上的用法介绍:
// 为所有assertThat和工具方法提供唯一访问入口(例如entry)
import static org.assertj.core.api.Assertions.*;
// 下面的示例中,fellowshipOfTheRing是一个TolkienCharacter列表
// 简单断言
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron)
.isIn(fellowshipOfTheRing);
// String断言
assertThat(frodo.getName()).startsWith("Fro")
.endsWith("do")
.isEqualToIgnoringCase("frodo");
// 集合断言
assertThat(fellowshipOfTheRing).hasSize(9)
.contains(frodo, sam)
.doesNotContain(sauron);
// 使用extracting特性检查fellowshipOfTheRing中的名字:)
assertThat(fellowshipOfTheRing).extracting("name").contains("Boromir", "Gandalf", "Frodo", "Legolas")
.doesNotContain("Sauron", "Elrond");
// Java 8方式的extracting
assertThat(fellowshipOfTheRing).extracting(character -> character.getRace().getName())
.contains("Hobbit", "Elf")
.doesNotContain("Orc");
// 断言之前过滤集合
assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT)
.containsOnly(sam, frodo, pippin, merry);
// 使用Java 8 lambda predicate过滤集合
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
.containsOnly(aragorn, frodo, legolas, boromir);
// 将extraction和过滤结合(是的,我们可以做到)
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
.containsOnly(aragorn, frodo, legolas, boromir)
.extracting(character -> character.getRace().getName())
.contains("Hobbit", "Elf", "Man");
个人常用到的一些方法:
String 校验
@Test
public void testString() {
String str = null;
// 断言null或为空字符串
assertThat(str).isNullOrEmpty();
// 断言空字符串
assertThat("").isEmpty();
// 断言字符串相等 断言忽略大小写判断字符串相等
assertThat("Frodo").isEqualTo("Frodo").isEqualToIgnoringCase("frodo");
// 断言开始字符串 结束字符穿 字符串长度
assertThat("Frodo").startsWith("Fro").endsWith("do").hasSize(5);
// 断言包含字符串 不包含字符串
assertThat("Frodo").contains("rod").doesNotContain("fro");
// 断言字符串只出现过一次
assertThat("Frodo").containsOnlyOnce("do");
// 判断正则匹配
assertThat("Frodo").matches("..o.o").doesNotMatch(".*d");
}
数字校验
@Test
public void testNumber() {
Integer num = null;
// 断言空
assertThat(num).isNull();
// 断言相等
assertThat(42).isEqualTo(42);
// 断言大于 大于等于
assertThat(42).isGreaterThan(38).isGreaterThanOrEqualTo(38);
// 断言小于 小于等于
assertThat(42).isLessThan(58).isLessThanOrEqualTo(58);
// 断言0
assertThat(0).isZero();
// 断言正数 非负数
assertThat(1).isPositive().isNotNegative();
// 断言负数 非正数
assertThat(-1).isNegative().isNotPositive();
}
时间断言
@Test
public void testDate() {
// 断言与指定日期相同 不相同 在指定日期之后 在指定日期之钱
assertThat(Dates.parse("2014-02-01")).isEqualTo("2014-02-01").isNotEqualTo("2014-01-01")
.isAfter("2014-01-01").isBefore(parse("2014-03-01"));
// 断言 2014 在指定年份之前 在指定年份之后
assertThat(DateTime.now().toDate()).isBeforeYear(2020).isAfterYear(2013);
// 断言时间再指定范围内 不在指定范围内
assertThat(parse("2014-02-01")).isBetween("2014-01-01", "2014-03-01").isNotBetween(
parse("2014-02-02"), parse("2014-02-28"));
List断言
@Test
public void testList() {
// 断言 列表是空的
assertThat(newArrayList()).isEmpty();
// 断言 列表的开始 结束元素
assertThat(newArrayList(1, 2, 3)).startsWith(1).endsWith(3);
// 断言 列表包含元素 并且是排序的
assertThat(newArrayList(1, 2, 3)).contains(1, atIndex(0)).contains(2, atIndex(1)).contains(3)
.isSorted();
// 断言 被包含与给定列表
assertThat(newArrayList(3, 1, 2)).isSubsetOf(newArrayList(1, 2, 3, 4));
// 断言 存在唯一元素
assertThat(Lists.newArrayList("a", "b", "c")).containsOnlyOnce("a");
}
Map断言
@Test
public void testMap() {
Map foo = Maps.newHashMap();
foo.put("A", 1);
foo.put("B", 2);
foo.put("C", 3);
// 断言 map 不为空 size
assertThat(foo).isNotEmpty().hasSize(3);
// 断言 map 包含元素
assertThat(foo).contains(entry("A", 1), entry("B", 2));
// 断言 map 包含key
assertThat(foo).containsKeys("A", "B", "C");
// 断言 map 包含value
assertThat(foo).containsValue(3);
}
添加错误提示信息: as
assertThat("abc").as("校验abc").isEqualTo("abcd");
错误提示:
org.junit.ComparisonFailure: [校验abc] expected:<"abc[d]"> but was:<"abc[]">
链式校验
// isEqualTo 相等, contains包含
assertThat("abc").as("校验abc").isEqualTo("abc").contains("d");
错误提示:
FAILED: testAssertJ
java.lang.AssertionError: [校验abc]
Expecting:
<"abc">
to contain:
<"d">
大小校验
List list = new ArrayList();
assertThat(list).hasSize(1);
FAILED: testAssertJ
java.lang.AssertionError:
Expected size:<1> but was:<0> in:
<[]>
null判断
Object object = null;
assertThat(object).isNotNull();
assertThat(object).isNull();
同时,AssertJ还支持自定义断言,就是根据自己的需求,自定义断言类型。