Spring Junit4 Test
捣鼓了差不多一天。。。终于把"No Session found for current thread"问题解决了
环境:Spring 4.0.6 RELEASE + Hibernate 4.2.2 Final
折腾记录如下:
1. 出现"No Session found for current thread",查找配置文件中的sessionFactory配置,确认无误;
2. 检查写的测试用例,并尝试修改注解:@Transactional和@TransactionConfiguration,没解决;
3. 再检查DAO层代码和对应的entity,确认没问题;
4. 搜索"No Session found for current thread",有人说是配置文件中需要加上<prop key="hibernate.current_session_context_class">thread</prop>
试了,结果没有了"No Session found for current thread",但是出现了"HibernateException: contains is not valid without active transaction",表明没有事务,错误更大了。
5. 接着搜索,找到如下blogs:
http://www.iteye.com/topic/1126047
根据以上博客内容,加上Service层代码,并测试通过,郁闷了。。。
http://blog.csdn.net/funi16/article/details/8691575
在看到这个博客后,噢了一声,果断把extends AbstractJUnit4SpringContextTests换成extends AbstractTransactionalJUnit4SpringContextTests,这才把事务管理加进来了,也可以回滚了!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext_persistence.xml")
@TransactionConfiguration(transactionManager = "transactionManager",defaultRollback = true)
public class ActionDAOImplTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired
private ActionService actionServiceImpl; @Autowired
private ActionDAO actionDAOImpl; @Test
//@Rollback
public void testAdd() throws Exception {
Action action = new Action();
action.setLoginDate(new Date());
Thread.sleep(2000);
action.setLogoffDate(new Date());
action.setUserName("chris");
action.setOperation("add;update;select");
actionServiceImpl.recordAction(action);
Action lookUpOne = actionServiceImpl.checkAction(4);
Assert.assertEquals("right","add;update;select",lookUpOne.getOperation());
} @Test
@Rollback(value = false)
public void testAdd2() throws Exception {
Action action = new Action();
action.setLoginDate(new Date());
Thread.sleep(2000);
action.setLogoffDate(new Date());
action.setUserName("chris");
action.setOperation("add;update;select");
actionDAOImpl.save(action);
Action lookUpOne = actionDAOImpl.find(8);
Assert.assertEquals("right","add;update;select",lookUpOne.getOperation());
}
}
Spring Junit4 Test的更多相关文章
- Spring Junit4 接口测试
Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...
- 使用Spring+Junit4.4进行测试(使用注解)
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 用Spring+Junit4.4进行测试(使用注解)
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 使用Spring+Junit4.4进行测试
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 使用Spring+Junit4进行测试
前言 单元测试是一个程序员必备的技能,我在这里就不多说了,直接就写相应的代码吧. 单元测试基础类 import org.junit.runner.RunWith; import org.springf ...
- Spring+JUnit4单元测试入门
(一).JUnit介绍 JUnit是Java中最有名的单元测试框架,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. Maven导入juni ...
- spring junit4 单元测试运行正常,但是数据库并无变化
解决方案 http://blog.csdn.net/molingduzun123/article/details/49383235 原因:Spring Juint为了不污染数据,对数据的删除和更新操作 ...
- spring junit4 测试
@Service @ContextConfiguration(locations = { "classpath:config/applicationContext.xml" }) ...
- easymock+junit+spring学习·
Easymock学习 Author:luojie 1. Easymock简介 EasyMock 是一套通过简单的方法对于指定的 ...
随机推荐
- Kettle 合并记录报错!
在Kettle的合并记录过程的时候,在“为了转换解除补丁开始 ”这一步的时候报错.具体错误如图所示: Kettle的转换如图所示: 问题原因:可能是你的数据库链接驱动和Kettle的版本不兼容. 解决 ...
- IMAP收邮件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 在xml中调用自己用java代码定义的View
1.在res中new一个class继承view.View,重写ondraw方法,写出自己的view package com.zzw.myView; import android.content.Con ...
- js实现选项卡功能
1.css .liclick{ border: 1px black solid; background: #fff; float: left; width: 80px; height: 35px; l ...
- 导购效果跟踪: SPM
什么是SPM SPM是淘宝社区电商业务(xTao)为外部合作伙伴(外站)提供的一套跟踪引导成交效果数据的解决方案. 下面是一个跟踪点击到宝贝详情页的引导成交效果数据的SPM示例: http://det ...
- Kafka入门学习随记(二)
====Kafka消费者模型 参考博客:http://www.tuicool.com/articles/fI7J3m --分区消费模型 分区消费架构图 图中kafka集群有两台服务器(Server), ...
- jquery Mobile应用第2课《构建跨平台APP:jQuery Mobile移动应用实战》连载二(简单的QWER键盘)
在jQuery Mobile的布局中,控件大多都是单独占据页面中的一行,按钮自然也不例外,但是仍然有一些方法能够让多个按钮组成一行,比如说在范例6-5中就利用按钮分组的方法使4个按钮并列在一行中,如图 ...
- [terry笔记]Oracle10g/11g安装-redhat5.5
操作系统准备工作 其实真正的进入图形界面安装是简单的,只是前期的配置比较繁琐,如版本.空间.位置.软件包.用户.环境变量.内核参数,这里主要讨论前期配置. 10g与11g的单实例安装基本一样,主要区别 ...
- hdu 2648 Shopping
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648 纯暴力的方法T_T... 如下: #include<cstdio> #include ...
- Python: 迭代器与生成器小结
迭代器与生成器的区别: 1. 迭代器由Class对象创建. 生成器由包含yield表达的Function对象或者Generator Expression创建. 2. 迭代器的原理: (1)由Itera ...