一、软件下载地址(本人使用的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 { }
@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之后,如下生成主键的代码都会报错:
Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().
据说要修改成这样才不会报错:
原因是:貌似从5.1.7版本之后的mysql-connector增加了返回GeneratedKeys的条件,如果需要返回GeneratedKeys,则PreparedStatement需要显示添加一个参数Statement.RETURN_GENERATED_KEYS。不过直接报错,不向下兼容也太不厚道了。
相关Bug信息:http://bugs.mysql.com/bug.php?id=41448