使用Spring TestContext 测试框架,完美测试基于Spring的应用程序
来源:互联网 发布:n个球放入m个盒子C语言 编辑:程序博客网 时间:2024/06/09 20:33
很长时间没有写过我的个人blog了,今天想记录一下刚刚使用Spring TestContext的心得。
至于Spring TestContext是个什么东西,各位只要google下就可以知道了。Spring TestContext为什么好?我只说一点,只使用Junit进行单元测试时,数据库现场容易遭受破坏, 而Spring TestContext刚可以很好的做到单元测试后恢复现场,使用的是事务回滚机制。
经过本人的实际使用,现在简单说明一下Spring TestContext的使用:
1,加jar包:
如果你使用的是spring2.5.6,请把/spring-framework-2.5.6/dist/modules/spring-test.jar copy到你的web项目的lib下。
2,写测试类,要从AbstractTransactionalJUnit4SpringContextTests继承。
package com.gxing.registration.service.impl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import com.gxing.registration.model.User;
import com.gxing.registration.service.UserManager;
@ContextConfiguration
public class UserManagerImplTest extends AbstractTransactionalJUnit4SpringContextTests{
@Autowired
private UserManager userManager;
@Test
public void testAdd() {
//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//UserManager um = (UserManager) ctx.getBean("userManager");
User u = new User();
u.setUsername("testService");
u.setPassword("testPassword");
userManager.add(u);
}
}
3,加上如测试代码中的Annotion,不明白就google,我不作解释,把这个权利留着给google老师。
3,编写UserManagerImplTest-context.xml文件,与上面的文件放到同一个包中
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">
<import resource="classpath:/beans.xml"/>
</beans>
4,把Juint4.4以上的jar包copy进web-info下的lib中。一定要注意是4.4以上的版本才支持。
5,Run as Juint Test,看看,测试通过了,但是我们的数据库里没有增加一个记录,看看控制台的Logger,你就明白了什么回事。
- 使用Spring TestContext 测试框架,完美测试基于Spring的应用程序
- 使用 Spring TestContext 测试框架
- Spring TestContext 测试框架
- Spring TestContext测试框架
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- Spring基于注解TestContext 测试框架使用详解
- 使用 Spring 2.5 TestContext 测试框架
- 使用 Spring 2.5 TestContext 测试框架
- 使用 Spring 2.5 TestContext 测试框架
- 使用 Spring 2.5 TestContext 测试框架
- Strtus2的ValueStack、ActionContext及Stack Context
- XML简介
- linux中的_init应用,含有__attribute__ 和section的意义和作用
- Column count doesn't match value count at row 1
- 编译原理 实验3 递归下降语法分析程序设计
- 使用Spring TestContext 测试框架,完美测试基于Spring的应用程序
- Linux网络编程必看书籍推荐
- 14.android向web提交参数的4种方式案例
- SQL存储过程分页若干方法
- AS3精辟代码若干--random
- 黑马程序员——java学习7(小日历制作)
- csdn apache+php+mysql在windows下的安装和配置
- 数据保存时,出现‘record not found or changed by another user’错误信息的解决办法
- JUnit学习笔记