AssertJ断言系列-----------<数据库断言二>
那么,在实际的接口测试中,我们除了要断言响应的数据正确之外,可能有的还需要断言数据层是否数据真的有入库。
assertj db是可以直接对数据库进行断言和操作的。
一、创建一个students表
CREATE TABLE `students` (
`id` VARCHAR(32) DEFAULT NULL,
`Name` VARCHAR(32) DEFAULT NULL,
`Sex` VARCHAR(32) DEFAULT NULL,
`Age` VARCHAR(32) DEFAULT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8
二、插入三条数据
insert into `students` (`id`, `Name`, `Sex`, `Age`) values('1','hello','1','11');
insert into `students` (`id`, `Name`, `Sex`, `Age`) values('2','world','2','22');
insert into `students` (`id`, `Name`, `Sex`, `Age`) values('3','java','3','33');
三、编辑代码
package org.assertj.examples.db;
import org.assertj.db.type.Request;
import org.assertj.db.type.Source;
import org.junit.BeforeClass;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import static org.assertj.db.api.Assertions.assertThat;
/**
* @ClassName DataAssertionExample
* @Description TODO
* @Author 12457
* @Date 2019/1/27 10:43
* @Version 1.0
**/
public class DataAssertionExample {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/course";
private static String username = "****"; //实际用户名
private static String password = "****"; //实际密码
private static Connection conn = null;
/*
*数据库配置连接
**/
@BeforeClass
public static void beforeClass(){
try {
Class.forName(driver);
conn = (Connection) DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void basic_request_assertion_examples(){
Source source = new Source(url,username,password);
Request request = new Request(source,"SELECT * FROM students");
//获取该表的字段名
List<String> list = request.getColumnsNameList();
for (String s : list) {
System.out.print(s + "\t");
}
//断言name这一列数据
assertThat(request).column("name")
.value().isEqualTo("hello")
.value().isEqualTo("world")
.value().isEqualTo("java");
//断言数据库表中的数据条数
assertThat(request).hasNumberOfRows(3);
//断言第一行的数据的值,row(0)代表第一行数据
assertThat(request).row(0)
.value().isEqualTo("1")
.value().isEqualTo("hello")
.value().isEqualTo("1")
.value().isEqualTo("11");
}
@Test
public void basic_column_request_assertion_examples(){
Source source = new Source(url,username,password);
Request request = new Request(source,"SELECT * FROM students");
//断言sex列中数据
assertThat(request).column("sex")
.hasValues("1","2","3");
}
@Test
public void basic_row_request_assertion_examples(){
Source source = new Source(url,username,password);
Request request = new Request(source,"SELECT * FROM students");
assertThat(request).row(1)
.hasValues("2","world","2","22");
}
@Test
public void size_request_assertion_examples(){
Source source = new Source(url,username,password);
Request request = new Request(source,"SELECT * FROM students");
//表有4列3行数据
assertThat(request).hasNumberOfColumns(4);
assertThat(request).hasNumberOfRows(3);
//另外相等的一种写法
assertThat(request).column().hasNumberOfRows(3);
assertThat(request).row().hasNumberOfColumns(4);
}
@Test
public void request_parameters_examples(){
Source source = new Source(url,username,password);
Request request = new Request(source,"SELECT name,sex FROM students where name like ?","%o%");
//查询出来有2条数据符号,然后对values值进行判断
assertThat(request).hasNumberOfColumns(2).hasNumberOfRows(2);
assertThat(request)
.row().hasNumberOfColumns(2).hasValues("hello","1")
.row().hasValues("world","2");
}
}