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 是一套通过简单的方法对于指定的 ...
随机推荐
- Hadoop伪分布式搭建CentOS
所需软件及版本: jdk-7u80-linux-x64.tar.gz hadoop-2.6.0.tar.gz 1.安装JDK Hadoop 在需在JDK下运行,注意JDK最好使用Oracle的否则可能 ...
- 共享内存shared pool (4):Library cache 转储文件
上一篇blog只是从概念上理解Library cache,本篇则是将Library cache从内存中dump出来,看看其结构. 基本命令 ALTER SESSION SET EVENTS 'imme ...
- 枚举esum20160530
关于枚举 常见定义形式,类似定义结构体,先定义枚举变量类型: typedef enum{ Bit_RESET = 0, Bit_SET}BitAction; enum box{pencil,pen ...
- Python脚本控制的WebDriver 常用操作 <八> 简单的对象定位
这一部分的内容,将是在WebDriver中,定位元素方法的演示,是将Selenium中Selenese元素定位命令的WebDriver中使用方法的结合 Selenium中元素定位方法复习可以参考: & ...
- WPF中线性渐变画刷的一个小窍门
最近被项目里面控件的设计搞的死去活来的,大部分的设计都会需要使用进度条的功能,因为UI形状的变态,使用ProgressBar不能满足需求,没办法就自己想办法实现进度显示.折腾的多了发现一个很不错的方法 ...
- wpf 在引用外部的资源字典
启动的APP.xaml
- next permutaion算法
算法描述: Find largest index i such that array[i − 1] < array[i]. Find largest index j such that j ≥ ...
- lnmp的使用
命令 1.状态管理 lnmp {start|stop|reload|restart|kill|status} 2.添加虚拟host lnmp vhost add
- Python数据类型-----数字&字符串
Python数字类型 int类型表示的范围:-2147483648至2147483648之间,超出这个范围的数字即视为long(长整形) 在Python中不需要事先声明数据类型,它是根据具体的赋值来进 ...
- 使用checked关键字处理“溢出”错误
在进行算术运算时,可以使用checked关键字有效处理溢出错误,使用checked关键字可能对程序的性能会有一点点的影响,这是一种以牺牲性能换取健康的做法. private void button1_ ...