如何深入掌握AssertJ断言库在Java中的原理与使用技巧?

2026-04-30 04:362阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计623个文字,预计阅读时间需要3分钟。

如何深入掌握AssertJ断言库在Java中的原理与使用技巧?

AssertJ 是我目前见过的最强大的断言API,没有之一。官网传送门 + 为什么使用AssertJ? + 1. 流式断言,代码即时示例,直观易懂。 + 例如: + 传统JUnit或TestNG,判断一个字符串是否包含字符串 + AssertJ断言示例:

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

官网传送门

如何深入掌握AssertJ断言库在Java中的原理与使用技巧?

为什么使用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; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计623个文字,预计阅读时间需要3分钟。

如何深入掌握AssertJ断言库在Java中的原理与使用技巧?

AssertJ 是我目前见过的最强大的断言API,没有之一。官网传送门 + 为什么使用AssertJ? + 1. 流式断言,代码即时示例,直观易懂。 + 例如: + 传统JUnit或TestNG,判断一个字符串是否包含字符串 + AssertJ断言示例:

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

官网传送门

如何深入掌握AssertJ断言库在Java中的原理与使用技巧?

为什么使用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; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。