捣鼓了差不多一天。。。终于把"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的更多相关文章

  1. Spring Junit4 接口测试

    Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...

  2. 使用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  3. 用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  4. 使用Spring+Junit4.4进行测试

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  5. 使用Spring+Junit4进行测试

    前言 单元测试是一个程序员必备的技能,我在这里就不多说了,直接就写相应的代码吧. 单元测试基础类 import org.junit.runner.RunWith; import org.springf ...

  6. Spring+JUnit4单元测试入门

    (一).JUnit介绍 JUnit是Java中最有名的单元测试框架,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. Maven导入juni ...

  7. spring junit4 单元测试运行正常,但是数据库并无变化

    解决方案 http://blog.csdn.net/molingduzun123/article/details/49383235 原因:Spring Juint为了不污染数据,对数据的删除和更新操作 ...

  8. spring junit4 测试

    @Service @ContextConfiguration(locations = { "classpath:config/applicationContext.xml" }) ...

  9. easymock+junit+spring学习·

    Easymock学习                                Author:luojie 1.       Easymock简介 EasyMock 是一套通过简单的方法对于指定的 ...

随机推荐

  1. 仿淘宝颜色属性选择展示代码(jQuery)

    模仿淘宝商品选择颜色和尺寸的效果,即选择商品颜色和尺寸的时候,把选择的颜色和尺寸放到一个页面容器里面,不足之处,还望指教. <!DOCTYPE HTML> <html lang=&q ...

  2. sqoop的job工具

    sqoop job: Work with saved jobs 就是将sqoop的某条语句保存为一个job 1.把person表导入到HDFS上,可以使用下面的语句 sqoop import --co ...

  3. List GetEnumerator

    static void Main() { List<int> list = new List<int>(); list.Add(); list.Add(); list.Add( ...

  4. sizeclass

    横竖9种组合,代表所有大小屏幕,在storyboard中可以把contrans与不同组合绑定,也就是说,可能横向有多一个约束,纵向就没了... 实现不同屏幕不同约束,这应该是sizeclass 的存在 ...

  5. [转]C++编写Config类读取配置文件

    //Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...

  6. Tutorial: Facebook analytics using Power BI Desktop

    In this tutorial you learn how to import and visualize data from Facebook. During the tutorial you'l ...

  7. golang的函数

    在golang中, 函数是第一类值(first-class object), 即函数可以赋值与被赋值. 换言之, 函数也可以作为ReceiverType, 定义自己的method. 实例: http. ...

  8. 条款2:尽量以const、enum、inline替换#define

    1> 以const替换#define • 比如用const double Ratio = 1.653替换#define RATIO 1.653 因为宏定义在预处理阶段就会被替换成其所指代的内容, ...

  9. DSP280x的数模转换使用

    /*****************************************************************************Copyright: 2013File na ...

  10. [原]项目进阶 之 集群环境搭建(二)MySQL集群

    上次的博文中我们介绍了一下集群的相关概念,今天的博文我们介绍一下MySQL集群的相关内容. 1.MySQL集群简介 MySQL群集技术在分布式系统中为MySQL数据提供了冗余特性,增强了安全性,使得单 ...