zoukankan      html  css  js  c++  java
  • 【原】涉及数据库的单元测试-JTeser

    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

  • 相关阅读:
    小白学开发(iOS)OC_ 使用继承来扩充类(2015-08-07)
    UI组件之TextView及其子类(三)ToggleButton和Switch
    C++智能指针--shared_ptr
    HDU 1013 Digital Roots 题解
    对touch事件传递的简单理解
    【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
    Cocos2d-x 坐标系
    hdu1518 Square
    servlet3.0新特性
    OGNL表达式
  • 原文地址:https://www.cnblogs.com/seven7seven/p/4105109.html
Copyright © 2011-2022 走看看