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. Springboot Actuator之一:执行器Actuator入门介绍

    介绍 Spring Boot有四大神器,分别是auto-configuration.starters.cli.actuator,本文主要讲actuator.actuator是spring boot提供 ...

  2. bootstrap-table服务端分页操作

    由于数据库查询的数据过多,所以采取服务端分页的操作,避免一次性加载的数据量过多,导致页面加载缓慢. 后端数据的封装格式json数据 rows里的数据是当前页的数据,total是总条数: { " ...

  3. 怎样理解Node接口 / ParentNode接口 / ChildNode接口

    ParentNode 和 ChildNode可以理解为是Node的子集, 它对一些具有父节点或子节点的节点提供了一些额外的方法和属性, 比如: 1. 继承了ParentNode的接口有: 元素节点 / ...

  4. js之语句(条件语句,循环语句,跳转语句)

    一.条件语句 1.if语句 条件语句是通过判断指定表达式的值来决定执行还是跳过某些语句,这些语句是代码是“决策点”有时称之为“分支”. if语句是一种基本的控制语句,它让Javascript程序可以选 ...

  5. 一种移动端position:absolute布局:

    一种移动端position:absolute布局:   1.他父级不需要加上 position:relative; 如果父级不是不是body,则加position:absolute; 2.红色加量部分 ...

  6. 内网渗透之frp使用

    0x00 前言 nps相比上次已经介绍过了.但是他有一个致命缺点就是在scks5代理下会长连接一直不放开导致结果不准确.所以来讲讲frp的使用.frp虽然需要落地配置文件,但是扫描的结果还是很准确的. ...

  7. 开源Android 恶意软件Radio Balouch

    安全研究机构 ESET 首次发现了开源 Android 间谍软件在 Google Play  上的恶意信息窃取行为,并且在被删除后仍在Google Play 重复出现.据悉,第一个间谍软件是基于开源间 ...

  8. IPC之mq_sysctl.c源码解读

    // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (C) 2007 IBM Corporation * * Author: Cedric ...

  9. Alpha版本第一周小结

    姓名 学号 周前计划安排 每周实际工作记录 自我打分(百分制) HTB 061126 1.博客撰写,分配任务 2.编码实现各个模块的功能 1.撰写博客 2.已初步实现各个模块的功能,对某些数据处理还存 ...

  10. python 之多线程join()

    join()其实就是阻塞线程,控制线程的执行,从而控制住代码的执行顺序. 参照这篇文章:python3对多线程join的理解 通常都是,线程join()后,顺序执行join()后面的代码,如下面的例子 ...