你是否有这样的经历。当你遇到需要测试数据库相关操作的时候,在每次测试之前,反复的通过ui来改变数据库的数据。这件事情可以交给 ScaffoldUnit 来做。
当前只通过了 MySQL 的测试
Maven 依赖
<dependency> <groupId>org.crazycake</groupId> <artifactId>ScaffoldUnit</artifactId> <version>1.0.0-RELEASE</version> </dependency>
在项目的classpath 根目录下 创建 ScaffoldUnit.properties , 比如 src/main/resources
ScaffoldUnit.jdbc.url=jdbc:mysql://localhost:3306/sunit_test?useUnicode=true&characterEncoding=UTF-8 ScaffoldUnit.jdbc.username=root ScaffoldUnit.jdbc.password=123456
创建一个测试数据库 sunit_test 建立一个表 student
CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(32) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建类 HelloScaffoldUnitTest.java ,有一个测试方法 testBuild.
package org.crazycake.ScaffoldUnit; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import static org.hamcrest.CoreMatchers.*; import org.junit.Test; public class HelloScaffoldUnitTest { @Test public void testBuild() throws IOException, SQLException, ClassNotFoundException{ //1 build the scaffold data ScaffoldUnit.build(); //2 test your code Class.forName("com.mysql.jdbc.Driver"); Connection conn = null; conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sunit_test?useUnicode=true&characterEncoding=UTF-8","root", "123456"); Statement stat = conn.createStatement(); stat.execute("update student set name='ted' where name='jack'"); stat.close(); conn.close(); //3 assert your result ScaffoldUnit.dbAssertThat("select name from student where id=1", is("ted")); } }
ScaffoldUnit 需要一个json文件作为告诉ScaffoldUnit如何初始化数据库,在 test/resources 下建立跟你的java类一样的文件路径结构,比如你的测试类全名是 org.crazycake.ScaffoldUnit么你建立的 json文件路径是这样的 test/resources/org/crazycake/ScaffoldUnit/ScaffoldUnitTest.json.
{ "ms":[ { "n":"testBuild", "ts":[ { "t":"student", "rs":[ [ { "c":"id", "v":"1" }, { "c":"name", "v":"'jack'" } ] ] } ] } ] }
这个json文件告诉ScaffoldUnit 在做 testBuild 测试之前做两件事情
1. truncate sunit_hello . 2. 插入一条数据 id=3, name= ‘jack’.
运行测试用例,看日志
2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:47 - ScaffoldUnit.jdbc.url=jdbc:mysql://localhost:3306/sunit_test?useUnicode=true&characterEncoding=UTF-8 2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:50 - ScaffoldUnit.jdbc.username=root 2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:53 - ScaffoldUnit.jdbc.password=qwer1234 2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:73 - truncate table student 2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:73 - insert into student (id,name) values (1,'jack') 2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:104 - select name from student where id=1
ScaffoldUnit won’t recover the test data. Instead, it clean all data of the tables which test case will use and initialize the data.
介绍那个json文件
ms: 方法的集合
n: 方法名
ts: 表的集合
t: 表名
rs:需要插入的行集合
c: 需要插入的行的列
v: 值
如果你只想清理这个表的数据但是不插入数据,可以不写rs属性:
{ "t":"nemo_clean" }
ScaffoldUnit 只是进行单纯的字段拼接而已,所以如果是字符串的属性要加上单引号
{ "c":"name", "v":"'jack'" }
{ "ms": [{ "n": "testComeAndBiteMe", "ts": [{ "t": "nemo_hello", "rs": [[{ "c": "id", "v": "1" }, { "c": "name", "v": "'foo'" }], [{ "c": "id", "v": "2" }, { "c": "name", "v": "'bar'" }]] }] }, { "n": "testBuild", "ts": [{ "t": "nemo_hello", "rs": [[{ "c": "id", "v": "3" }, { "c": "name", "v": "'mike'" }], [{ "c": "id", "v": "4" }, { "c": "name", "v": "'kitty'" }]] }, { "t": "nemo_clean" }] }] }
在测试方法的第一行调用 ScaffoldUnit.build() 你还可以调用这些方法: comeAndBiteMe iHateWorkOvertime screwU myBossIsAMuggle and wtf. 效果跟 build(), 一样。
为了断言的方便 ScaffoldUnit 提供了 dbAssertThat . 这个方法会把你传入的sql的查询结果的第一行的第一个列的值拿出来比较。例子
import static org.hamcrest.CoreMatchers.*; ... ScaffoldUnit.dbAssertThat("select name from student where id=1", is("ted"));
ScaffoldUnit 提供了一个方式可以自动初始化数据结构. 创建一个 sql fileScaffoldUnit.sql 在classpath根目录下. 当ScaffoldUnit 构建测试数据的时候如果遇到表不存在的异常,会试着用这个文件创建数据结构
ScaffoldUnit.sql 的一个例子. 我是用SQLyog导出的这个sql文件。
/* SQLyog Ultimate v8.71 MySQL - 5.0.51b-community-nt : Database - nemo_test ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; /*Table structure for table `nemo_clean` */ DROP TABLE IF EXISTS `nemo_clean`; CREATE TABLE `nemo_clean` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `nemo_hello` */ DROP TABLE IF EXISTS `nemo_hello`; CREATE TABLE `nemo_hello` ( `id` int(11) NOT NULL, `name` varchar(32) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Table structure for table `student` */ DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(300) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;