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. java注解类型的aop

    import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import org.aspectj.la ...

  2. 嵌套泛型参数IList<IList<Object>>如何传参

    在调用第三方库的时候,有这么一个泛型参数,如下图: 按照经验,使用两个List嵌套声明变量即可: IList<IList<AnnotatedPoint2D>>  outImag ...

  3. tee、vi/vim命令

    一.tee:多重定向 语法:       tee [OPTION] ... [FILE] ... 参数:       将标准输入复制到每个FILE,也复制到标准输出. -a,--append      ...

  4. WEB小计

     使用vue的事件绑定时,应当使用.stop来阻止事件的传播   html 有捕获和冒泡两种事件机制

  5. Codeforces 1244G. Running in Pairs

    传送门 首先对于两个排列 $A,B$ 我们可以把 $A$ 从小到大排序并把 $B$ 重新和 $A$ 一一对应 显然这样不会影响 $\sum_{i=1}^{n}max(A_i,B_i)$ 的值 所以直接 ...

  6. 【web性能优化】当用户输入网址后发生了什么?

    简单叙述 这个过程可以大致分为两个部分:网络通信和页面渲染. 一.网络通信 互联网内各网络设备间的通信都遵循TCP/IP协议,利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信.分层由 ...

  7. B-Tree和 B+Tree的数据存储结构

    B+树索引是B+树在数据库中的一种实现,是最常见也是数据库中使用最为频繁的一种索引.B+树中的B代表平衡(balance),而不是二叉(binary),因为B+树是从最早的平衡二叉树演化而来的.在讲B ...

  8. python之字符串类型及其操作

    1.1字符串类型的表示 字符串是字符的序列表示,可以由一对单引号('). 双引号(")或三引号(’")构成.其中,单引号和双引号都可以表示单行字符串,两者作用相同.使用单引号时,双 ...

  9. Java 里 如何使用Base64,网上都是废物的说法

    百度搜索Java里如何使用Base64,结果很多文章都是让引用第三方Jar包,我靠我想了一下 他妈的Java里连这个都不提供,就直接忽略里那些废物的文章.继续搜索,算是找到答案: Java8以后 官方 ...

  10. scala 面向对象之 继承

    scala 面向对象之 继承 scala   1.extends Scala中,让子类继承父类,与Java一样,也是使用extends关键字 继承就代表,子类可以从父类继承父类的field和metho ...