在运行测试用例之前初始化所需的测试数据。
你是否有这样的经历。当你遇到需要测试数据库相关操作的时候,在每次测试之前,反复通过ui来改变数据库的数据。这件事情可以交给ScaffoldUnit来做。
当前只通过了MySQL的测试
Maven依赖
<dependency><groupId>org.crazycake</groupId><artifactId>ScaffoldUnit</artifactId><version>1.0.0-RELEASE</version></dependency>快速开始STEP1.创建ScaffoldUnit.properties在项目的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=123456STEP2.创建测试数据库创建一个测试数据库 sunit_test 建立一个表 student
CREATE TABLE `student` ( `id` int(11) NOT NULL, `name` varchar(32) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;STEP3.创建测试用例创建类 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")); } }STEP4.创建json文件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'.
STEP5.运行测试用例!运行测试用例,看日志
2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:47 - ScaffoldUnit.jdbc.url=jdbc:mysql://localhost:3306/sunit_test?useUnicode=true&characterEncoding=UTF-82014-09-21 17:55:37 DEBUG ScaffoldUnitDao:50 - ScaffoldUnit.jdbc.username=root2014-09-21 17:55:37 DEBUG ScaffoldUnitDao:53 - ScaffoldUnit.jdbc.password=qwer12342014-09-21 17:55:37 DEBUG ScaffoldUnitDao:73 - truncate table student2014-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'trecoverthetestdata.Instead,itcleanalldataofthetableswhichtestcasewilluseandinitializethedata.
xxxx.json介绍那个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(),一样。
数据库版本的assertThat为了断言的方便 ScaffoldUnit 提供了 dbAssertThat .这个方法会把你传入的sql的查询结果的第一行的第一个列的值拿出来比较。例子
import static org.hamcrest.CoreMatchers.*;... ScaffoldUnit.dbAssertThat("select name from student where id=1", is("ted"));自动初始化数据结构ScaffoldUnit 提供了一个方式可以自动初始化数据结构.创建一个sqlfileScaffoldUnit.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 */;
评论