java安利一下断言利器AssertJ

java安利一下断言利器AssertJ

AssertJ是我目前见过的最强大的断言api,没有之一。

官网传送门
为什么使用assertJ?
1、流式断言,代码即用例,直观易懂。
举个例子:
传统的junit或者testng,判断一个字符串包不包括a跟b两个字符。要这么写
assertTrue(stringbuffer.contains("a") && stringbuffer.contains("b"))

而如果你用的assertJ

assertThat(stringbuffer).contains("a").contains("b").as("判断字符串是否包括a|b")
  • 1

相比之下,显然后者更加容易理解。而且as的注释更是让断言清晰

2、方便定制的断言器
试想一下。当你在做接口测试的时候,还在到处写着

JSONPath.eval(JSONObject.parse(String),"$yourpath").tostring.equals(expectString)
  • 1

你的接口自动化里边。到处都是这些看都不想看的json解析,判断。然而,当你有了assertJ,你可以自定义你的断言,尽可能的简化你的测试代码,可读性将能几何倍数提升。下边是我自己写的一个针对json的自定义断言器:



import java.math.BigDecimal;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractBigDecimalAssert;
import org.assertj.core.api.AbstractBooleanAssert;
import org.assertj.core.api.AbstractCharSequenceAssert;
import org.assertj.core.api.AbstractIntegerAssert;
import org.assertj.core.api.Assertions;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;


/**
 * assertJ json数据判断增强 eg:不提供提取数组的方法,在断言中作用比较小
 * 
 * @author jacksoncina2008
 *
 */
public class AssertJSON extends AbstractAssert<AssertJSON, String> {

    protected AssertJSON(String actual) {
        super(actual, AssertJSON.class);
        // TODO Auto-generated constructor stub
    }

    public static AssertJSON assertThat(String json) {
        return new AssertJSON(json);
    }

    /**
     * 提取字符串节点
     */
    public AbstractCharSequenceAssert<?, String> jsonPathAsString(String path) {
        return Assertions.assertThat((String) JSONPath.eval(getJSON(actual), path));
    }

    /**
     * 提取boolean节点
     */

    public AbstractBooleanAssert<?> jsonPathAsBoolean(String path) {
        return Assertions.assertThat((boolean) JSONPath.eval(getJSON(actual), path));
    }



    /**
     * 提取数字节点
     *
     */
    public AbstractIntegerAssert<?> jsonPathAsInteger(String path) {
        return Assertions.assertThat((Integer) JSONPath.eval(getJSON(actual), path));
    }

    /**
     * 提取小数
     * 
     */

    public AbstractBigDecimalAssert<?> jsonPathAsBigDecimal(String path) {
        return Assertions.assertThat((BigDecimal) JSONPath.eval(getJSON(actual), path));
    }

    private JSONObject getJSON(String json) {
        JSONObject j = new JSONObject();
        j = JSONObject.parseObject(json);
        return j;
    }
}
  • 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

有了这个。你在引入assertJ之后。就可以这样组织你的断言了

AssertJSON.assertThat(response).jsonPathAsString.isEqualTo("xxx").as("比较xxx");
  • 1

3、引入了java8依赖的lamda特性
支持直接对常见的colletion做断言

assertThat(fellowshipOfTheRing).filteredOn( character -> character.getName().contains("o") )
                               .containsOnly(aragorn, frodo, legolas, boromir);
  • 1
  • 2

比如上面这里的意思就是,是伙伴关系的,而且名字里边包括“o”的人。只有aragorn, frodo, legolas, boromir.相当人性化的表达。如果单纯的用junit或者testng的断言。恐怕就要自己写一堆的循环跟限制才能表达清楚这个验证点了。

原文地址:https://www.cnblogs.com/xinxihua/p/13899723.html