一、软件下载地址(本人使用的spring是2.5版本)
1、spring-test.jar下载地址,我的是2.5版本
http://grepcode.com/snapshot/repo1.maven.org/maven2/org.springframework/spring-test/2.5
2、junit需要4.4版本,过高会出现java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException异常
http://sourceforge.net/projects/junit/files/junit/4.4/junit-4.4.jar/download
二、测试内容
1、使用基类,applicationContext-test其实跟applicationContext.xml一模一样,只是把他放入test包中
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
//告诉他我们需要载入的配置文件,例如@ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”})
@ContextConfiguration(locations={"/applicationContext-test.xml"})
public class BaseServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
}
ps:在我引入多个数据源的时候,上面运行会报错。主要原因是spring默认会使用type去匹配你注入的数据源,我的两个数据源类名是一样的,只是绑定的id不一样,spring不知道AbstractTransactionalJUnit4SpringContextTests注入所需的是哪个bean,所以这里需要使用
@Autowired
public void setDataSource(@Qualifier("cqDataSource") DataSource cqDataSource) {
super.setDataSource(cqDataSource);
}
2、测试用例
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
//默认会继承父类的中的spring配置文件,如果你想自己配置,重新写,如
//@ContextConfiguration(locations={"base-context.xml"})
public class NetStandardServiceTest extends BaseServiceTest {
//如果不使用注解,就按照之前的方式get和set的方法
@Autowired
private NetStandardService netStandardService;
private int netStandardId;
@Test//保存测试
public void saveTest() {
String name = "haiji";
NetStandard ns = new NetStandard();
ns.setName(name);
this.netStandardService.save(ns);
int key = ns.getNetStandardId();
NetStandard nst = this.netStandardService.get(NetStandard.class, key);
Assert.assertEquals("网络制式保存出错", name, nst.getName());
Assert.assertEquals("网络制式保存出错", "1", nst.getName());
}
//准备数据
@Before//准备测试数据,这里的before是跟其他test方法放入同一个事务中,若不想同一个事务,可以使用@BeforeTransaction
public void prepareTestData() {
final String sql = "insert into tbl_net_standard(name) values('1234')";
simpleJdbcTemplate.update(sql);
KeyHolder keyHolder = new GeneratedKeyHolder();
simpleJdbcTemplate.getJdbcOperations().update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection conn)
throws SQLException {
PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
return ps;
}
}, keyHolder);
netStandardId = keyHolder.getKey().intValue();//①-1 记录测试数据的id
}
@Test//查询测试
public void getTest() {
NetStandard ns = this.netStandardService.get(NetStandard.class, this.netStandardId);
Assert.assertEquals("查询出错", "1234", ns.getName());
Assert.assertEquals("查询出错", "1", ns.getName());
}
@Test//更新测试
public void updateTest() {
String updateName = "海鸡";
NetStandard ns = this.netStandardService.get(NetStandard.class, this.netStandardId);
ns.setName(updateName);
this.netStandardService.update(ns);
ns = this.netStandardService.get(NetStandard.class, this.netStandardId);
Assert.assertEquals("更新名称出错", updateName, ns.getName());
Assert.assertEquals("更新名称出错", "1234", ns.getName());
Assert.assertEquals("更新Id出错", this.netStandardId, ns.getNetStandardId().intValue());
}
@Test//删除测试
public void deleteTest() {
this.netStandardService.delete(NetStandard.class, this.netStandardId);
NetStandard ns = this.netStandardService.get(NetStandard.class, this.netStandardId);
Assert.assertEquals("删除出错", Object.class, ns);
Assert.assertNull("删除出错", ns);
}
}
3、对于新版本的spring(3.0后),你可以查看此处文档,http://static.springsource.org/spring/docs/3.0.0.RELEASE/reference/htmlsingle/spring-framework-reference.html#testcontext-framework你可以发现这里是使用这种更加方便的方式:
package com.blog.service;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContextTest.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class BlogServiceTest{
@Before
public void setup() {
}
@Test
public void testAddBlog() {
Assert.fail("no test");
}
}
请注意上面的
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/applicationContextTest.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)这个在配置文件中有定义事务
如果你想要注入其他src的对象,直接使用@resource方式注入进来。
参考内容:
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-test/
出现过一下问题:http://oraclestudy.iteye.com/blog/1232857
今天部署项目的时候顺便把一些依赖用新版本的jar替换了,其他还好就是MYSQL的驱动出了问题。
原来用的是5.1.5的包,替换成5.1.17之后,如下生成主键的代码都会报错:
- ps = getConnection().prepareStatement(sql.toString());
- ....
- rs = ps.getGeneratedKeys();
Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().
据说要修改成这样才不会报错:
- ps = getConnection().prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
- ...
- rs = ps.getGeneratedKeys();
原因是:貌似从5.1.7版本之后的mysql-connector增加了返回GeneratedKeys的条件,如果需要返回GeneratedKeys,则PreparedStatement需要显示添加一个参数Statement.RETURN_GENERATED_KEYS。不过直接报错,不向下兼容也太不厚道了。
相关Bug信息:http://bugs.mysql.com/bug.php?id=41448
参考:http://www.itsmile.net/blog/article_show/20/273752
分享到:
相关推荐
spring通过junit测试
Spring 测试所需的jar
web项目工程下,导入该jar包,可以在spring框架下进行相关代码的测试
有关spring测试、struts测试、hibernate测试、软件测试、网站测试、利用JBuilder测试的技术
给大家介绍一款非常实用、且高端大气上档次的spring测试
accp6.0 spring测试题
Spring测试框架.rarSpring测试框架.rarSpring测试框架.rar
我做的SPRING测试
spring测试类实现 具体内容参照http://www.cnblogs.com/a757956132/p/6072962.html
spring测试类实现 具体内容参照http://www.cnblogs.com/a757956132/p/6072962.html
spring单元测试包spring单元测试包spring单元测试包spring单元测试包spring单元测试包
spring测试类实现 具体内容参照http://www.cnblogs.com/a757956132/p/6072962.html
Spring环境搭建、Spring中Bean的配置(Bean注入的2种方式,注入细节(字面值、引用其他bean、内部bean、集合属性、util scheme 定义集合、p命名空间))
Thymeleaf+Spring 测试项目该项目的目的是研究 Spring 和 Thymeleaf 作为 Eionet 软件的新软件标准。 设置为E-PRTR的demo,供大家讨论改写使用。 该应用程序使用 Liquibase 创建和升级数据库,并使用 Thymeleaf 作为...
测试httpclient,主要测试json参数的接口,包括get请求和post请求。
spring测试示例
最小配置Spring-测试
用于代码测试,对测试类和方法进行单独测试和debug,高效率对各种方法进行测试
SSH(struts2,Hibernate,Spring)整合及测试亲测可用,百分之白亲测可用。
该资源包含了spring整合单元测试Junit开发包spring-test-4.2.4.RELEASE.jar