安利一下断言利器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")

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

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

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

你的接口自动化里边。到处都是这些看都不想看的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;
    }
}

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

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

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

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

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

目前我已经在我自己的自动化测试项目里边全部替换成了assertJ。后边我打算再扩展一下我的AssertJSON,然后放到给maven给更多的人使用。哈哈。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AssertJ 是 JAVA 的流畅断言库。示例代码:// unique entry point to get access to all assertThat methods and utility methods (e.g. entry) import static org.assertj.core.api.Assertions.*;  // common assertions assertThat(frodo.getName()).isEqualTo("Frodo"); assertThat(frodo).isNotEqualTo(sauron)                  .isIn(fellowshipOfTheRing);  // String specific assertions assertThat(frodo.getName()).startsWith("Fro")                            .endsWith("do")                            .isEqualToIgnoringCase("frodo");  // collection specific assertions assertThat(fellowshipOfTheRing).hasSize(9)                                .contains(frodo, sam)                                .doesNotContain(sauron);  // using extracting magical feature to check fellowshipOfTheRing characters name :) assertThat(fellowshipOfTheRing).extracting("name").contains("Boromir", "Gandalf", "Frodo", "Legolas")                                                   .doesNotContain("Sauron", "Elrond");  // map specific assertions, ringBearers initialized with the elves rings and the one ring bearers. assertThat(ringBearers).hasSize(4)                        .contains(entry(oneRing, frodo), entry(nenya, galadriel))                        .doesNotContainEntry(oneRing, aragorn);  // and many more assertions : dates, file, numbers, exceptions ... 标签:AssertJ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值