<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>
因为 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-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>
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
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));
}
}
该 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|
在单元测试中右击-> Debug/Run As -> TestNG Test
早先的 jTester 中提供了 DbFit 方式来准备和验证数据库数据, jTester 从1.1.6开始推出了一种新的数据库数据准备和验证的方法,即 DataMap 方式。DataMap 对比 DbFit 有如下优势:
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));
}
}
在单元测试中右击-> Debug/Run As -> jUnit Test
手机扫一扫
移动阅读更方便
你可能感兴趣的文章