JTeser方法之一:@DbFit

一、maven 依赖项

		<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jtester</groupId>
<artifactId>jtester</artifactId>
<version>1.1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>fitnesse</groupId>
<artifactId>fitnesse</artifactId>
<version>20100211</version>
<scope>test</scope>
</dependency>

二、安装 TestNG 插件

因为 jTester 默认是基于 TestNG 开发的,想要运行 jTester 的测试,需要安装 TestNG 的 eclipse 插件。

  • eclipse 中 Help -> Install New Software
  • 弹出 Install 窗口,在 Work with 中输入
testng - http://beust.com/eclipse
  • 选中下面Name框中出现的TestNG,一直Next,直到Finish
  • 安装完毕后,在Window->Preferences里面出现TestNG选项,表示安装成功

三、测试相关的配置文件

在 src/test/resources 目录下: spring-test-datasources.xml与 jtester.properties。

spring-test-datasources.xml

与正式环境的 spring-beans.xml 相同,只需要copy一份,并修改 PropertyPlaceholderConfigurer 为 jtester.properties。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="com.mz.service" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>jtester.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${database.driverClassName}"></property>
<property name="url"
value="${database.url}"></property>
<property name="username" value="${database.userName}"></property>
<property name="password" value="${database.password}"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="mapperLocations" value="classpath*:sql/**/*.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<!-- <constructor-arg index="1" value="BATCH" /> -->
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">
</property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(public * com.mz.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
</aop:config> </beans>

jtester.properties 文件:

database.type=mysql
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/test_db?characterEncoding=UTF-8
database.userName=root
database.password=root
database.schemaNames=test_db

四、DbFit测试用例

UserServiceTest.java

package com.mz.service;

import org.jtester.annotations.DbFit;
import org.jtester.annotations.SpringApplicationContext;
import org.jtester.annotations.SpringBeanByName;
import org.jtester.testng.JTester;
import org.junit.Assert;
import org.testng.annotations.Test; import com.mz.entity.User; @SpringApplicationContext({ "spring-test-datasources.xml" })
public class UserServiceTest extends JTester {
@SpringBeanByName("userService")
UserService userService; @Test
@DbFit(when = "UserService.loginCheck.when.wiki")
public void testLoginCheck() {
User user = new User();
user.setUsername("u1");
user.setPassword("p1");
Assert.assertEquals(true, userService.loginCheck(user));
}
}

UserService.loginCheck.when.wiki

该 wiki 必须放在 src/test/resources 中,且与 UserServiceTest.java 类具有相同目录结构。例如: UserServiceTest.java 位于 src/test/java 下的 com.mz.service 包中;则 UserService.loginCheck.when.wiki 应放在 src/test/resources 下的 com.mz.service 中。

|connect|
|clean table|user|
|insert|user|
|username|password|createTime|
|u1|p1|2011-11-11 11:11:11| |commit|

运行 DbFit 测试用例

在单元测试中右击-> Debug/Run As -> TestNG Test

JTeser方法之二:DataMap

早先的 jTester 中提供了 DbFit 方式来准备和验证数据库数据, jTester 从1.1.6开始推出了一种新的数据库数据准备和验证的方法,即 DataMap 方式。DataMap 对比 DbFit 有如下优势:

  • 准备和验证数据都是在 java 代码中,无需额外文件
  • 因为只有 java 代码,数据编辑更方便
  • 验证数据库数据和 jTester 中其他断言方式一致,错误信息直接显示在测试方法上
  • 只需关注自己感兴趣的字段,框架自动帮忙填充无关的字段
  • 构造数据灵活,可以 根据需要构造特定规则的数据

一、二、三、前三步骤同DbFit方式一样

四、DataMap测试用例

UserServiceTest.java

package com.mz.service;

import java.text.SimpleDateFormat;
import java.util.Date; import org.jtester.testng.JTester;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration; import com.mz.entity.User; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-test-datasources.xml" })
@TransactionConfiguration(transactionManager = "transactionManager")
public class UserServiceTest extends JTester {
@Autowired
UserService userService; private void init() {
db.table("user").clean();
final String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
.toString();
db.table("user").clean().insert(new DataMap() {
{
this.put("username", "u1");
this.put("password", "p1");
this.put("createTime", now);
}
}).commit();
} @Test
public void testLoginCheck() {
init();
User user = new User();
user.setUsername("u1");
user.setPassword("p1");
Assert.assertEquals(true, userService.loginCheck(user));
}
}

运行DataMap 测试用例

在单元测试中右击-> Debug/Run As -> jUnit Test

【原】涉及数据库的单元测试-JTeser的更多相关文章

  1. Web项目--------原Oracle数据库的项目同时兼容MySql

    原Oracle数据库的项目同时兼容MySql步骤: (一)修改资源配置文件applicationContext-dataSource.xml的数据库连接 Oracle数据库中加上from dual的原 ...

  2. Android开发学习——SQLite数据库与单元测试

    SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper  public class Myopenhelper extends SQLiteOpenHelp ...

  3. 使用H2数据库进行单元测试

    背景 H2 数据库是一个开源的嵌入型内存数据库,采用纯Java语言实现: 程序非常小巧轻便,整个完整的Jar包也只有1.5M左右,很容易集成到项目中. 官网地址 http://www.h2databa ...

  4. java~springboot~h2数据库在单元测试中的使用

    单元测试有几点要说的 事实上springboot框架是一个tdd框架,你在进行建立项目时它会同时建立一个单元测试项目,而我们的代码用例可以在这个项目里完成,对于单元测试大叔有以下几点需要说明一下: 单 ...

  5. kotlin + springboot整合mybatis操作mysql数据库及单元测试

    项目mybatis操作数据库参考: http://how2j.cn/k/springboot/springboot-mybatis/1649.html?p=78908 junit对controller ...

  6. ASP.NET MVC5--为数据库新增字段(涉及数据库迁移技术)

    Setting up Code First Migrations for Model Changes--为模型更改做数据库迁移. 1.打开资源管理器,在App_Data文件夹下,找到movies.md ...

  7. [原]DbHelper-SQL数据库访问助手

    using System; using System.Data; using System.Data.SqlClient; namespace Whir.Software.Framework.Ulti ...

  8. php涉及数据库操作时响应很慢。

    症状描述: 网站是php开发的,大部分页面响应很慢. 本地开发时响应速度很快,但是部署到生产环境后大部分响应很慢. 通过谷歌浏览调试发现PHP页面加载很慢,有个别的php请求的响应时间甚至超过10秒, ...

  9. 【原】数据库SQL语句入门

    1.数据定义DDL(Data Definition Language)语言即对表结构的一些定义,主要包括动词为CREATE/DROP/ALTER. 1.1.CREATE语句 CREATE TABLE ...

随机推荐

  1. redis 发布订阅、geo、bitmap、hyperloglog

    1.发布订阅 简介 发布订阅类似于广播功能.redis发布订阅包括 发布者.订阅者.Channel 命令 命令 作用 时间复杂度 subscribe channel 订阅一个频道 O(n) unsub ...

  2. 在Ubuntu上安装hadoop-2.7.7

    1.安装open-vm-tools sudo apt-get install open-vm-tools 2.安装openjdk sudo apt-get install openjdk-8-jdk ...

  3. Magazine Delivery(POJ1695)【DP】

    题意:要求用三辆车往n座城市投递货物,起点都在一号城市,每辆车可以载任意数量的货物,投递顺序必须与城市编号递增序一致,并且,每次同时都只能有一辆车在跑路.求最短总路径之和. 思路:每时每刻,能够充分决 ...

  4. Thinkphp5 自定义上传文件名

    这几天在做tp5的上传文件模块,项目需求是要把文件名在上传之后修改为 用户名+原文件名的组合形式,在网上找了一会儿发现好像没有类似的文章...只好自己去研究研究了. 之前查看过看云上面的官方手册,文件 ...

  5. 并不对劲的复健训练-p5212 SubString

    题目大意 有一个串\(s\),一开始只知道它的一个前缀.有\(q\)(\(q\leq 10^4\))个操作,操作有两种:1.给一个字符串,表示\(s\)(\(s\)总长\(\leq 6\times 1 ...

  6. [C#.net]xlApp.Workbooks.Open打开无法远程访问

    上月还能使用的xlApp.Workbooks.Open,这个月报无法远程访问,搞了半天,才找到原因是Foxit PDF 的Execl插件搞的鬼,记录下 Excel.Workbooks wbChecks ...

  7. 【weixin】微信支付---PC网站微信支付

    一.PC网站支付 微信支付支持完成域名ICP备案的网站接入支付功能.PC网站接入支付后,可以通过JSAPI支付或Native支付,自行开发生成二维码,用户使用微信“扫一扫”来完成支付. 二.支付产品介 ...

  8. maven:无效的目标发行版:11

    maven:无效的目标发行版:11 我之前在博客里是不记录bug和error的处理的,昨天听了一个资深程序员的视频,决定要改习惯了,记录一些自己平时遇到的问题 这个是我在mvn clean insta ...

  9. shell、bash、terminal和kernel之间的关系

    shell.bash.terminal和kernel之间的关系 本文是<鸟哥的Linux私房菜>的学习笔记 什么是kernel kernel的中文是"操作系统核心",主 ...

  10. Excel学习笔记:行列转换

    目录 offset函数 行列转换的三种方式 1.右键转置 2.转置公式TRANSPOSE 3.引用函数OFFSET+ROWS/COLUMN(支持随时更新数据) 一行(列)转多行多列 offset函数 ...