本帖已被设为精华帖!,
1.背景
在设计自动化cases时,遵守的核心原则是3A(Arrange-> Actor ->Assert)原则;
断言工具是否强大直接影响到用例的执行效率,本文将介绍一种流式断言神器:AssertJ。
在JAVA cases中常用的断言工具有JUnit自带的Assert,还有hamcrest的assertThat(之前总结过http://blog.csdn.net/neven7/article/details/42489723),这些工具对于AssertJ来说都相形见绌。
2.AssertJ介绍
AseertJ: JAVA 流式断言器,什么是流式,常见的断言器一条断言语句只能对实际值断言一个校验点,而流式断言器,支持一条断言语句对实际值同时断言多个校验点。
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.
3.AssertJ使用
本文将主要介绍Core下常用的断言方法。
版本要求:
AssertJ major versions depend on different Java versions :
AssertJ 1.x requires Java 6 or higher (suitable for Android)
AssertJ 2.x requires Java 7 or higher (should be suitable for Android - please file an issue if it's not the case)
AssertJ 3.x requires Java 8 or higher (not suitable for Android due to Path assertions)
本文使用JDK7, maven pom.xml添加依赖:
org.assertj
assertj-core
2.4.1
test
在cases中import assertj
import static org.assertj.core.api.Assertions.*;
先介绍一个实例,比较类对象,用到了isEqualToComparingFieldByFieldRecursively,嵌套比较字段值。
package com.weibo.qa.testcase.lab;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.*;
/**
*
* 两个类对象比较
*
* @author hugang
*
*/
class A {
List list = new ArrayList();
String str;
}
class B {
A aB = new A();
String test;
}
public class AssertJTest {
@Test
public void testAssertJ() {
A a1 = new A();
A a2 = new A();
a1.str = "hello";
a2.str = "hello";
a1.list.add("world");
a2.list.add("world");
B b1 = new B();
B b2 = new B();
b1.test = "b1";
b2.test = "b1";
b1.aB.list = a1.list;
b1.aB.str = a1.str;
b2.aB.list = a2.list;
b2.aB.str = a2.str;
// 不包含 对象类型
assertThat(a1).isEqualToComparingFieldByField(a2);
// 对象类型 只比较引用
// assertThat(b1).isEqualToComparingFieldByField(b2);
// 含有嵌式比较,含有对象类型的字段,再比较对象内的数据
assertThat(b1).isEqualToComparingFieldByFieldRecursively(b2);
}
}
常用的一些用法:
添加错误提示信息: as
assertThat("abc").as("校验abc").isEqualTo("abcd");
错误提示:
org.junit.ComparisonFailure: [校验abc] expected: but was:
链式校验.
// isEqualTo 相等, contains包含
assertThat("abc").as("校验abc").isEqualTo("abc").contains("d");
错误提示:
FAILED: testAssertJ
java.lang.AssertionError: [校验abc]
Expecting:
to contain:
null判断
Object object = null;
assertThat(object).isNotNull();
AILED: testAssertJ
java.lang.AssertionError:
Expecting actual not to be null
Object object = null;
assertThat(object).isNull();
是否在某个范围内
List list = new ArrayList();
assertThat(list).isIn(new ArrayList(), new HashMap());
assertThat(list).isNotIn(new ArrayList(), new HashMap());
java.lang.AssertionError:
Expecting:
not to be in:
大小
List list = new ArrayList();
assertThat(list).hasSize(1);
FAILED: testAssertJ
java.lang.AssertionError:
Expected size:<1> but was:<0> in:
等等,实在太多了,等你来发掘。
可参见官方wiki: http://joel-costigliola.github.io/assertj/index.html
原创文章,作者:华为小猪,如若转载,请注明出处:http://www.stest.com