捣鼓了差不多一天。。。终于把"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. java基础知识梳理

    java基础知识梳理 1 基本数据类型

  2. Oracle与SQL Server事务处理的比较

    事务处理是所有大型数据库产品的一个关键问题,各数据库厂商都在这个方面花费了很大精力,不同的事务处理方式会导致数据库性能和功能上的巨大差异.事务处理也是数据库管理员与数据库应用程序开发人员必须深刻理解的 ...

  3. web.xml中常见配置解读

    文章转自:http://blog.csdn.net/sdyy321/article/details/5838791 有一般XML都必须有的版本.编码.DTD <web-app>下子元素&l ...

  4. shell字符串的截取

    1.变量 var 从 npos ∈ [0, length-1] 位开始,从左->右截取 num 个字符: ${var:npos:num} / ${var:npos} 小结:若 npos < ...

  5. ORACLE-12C-RAC INSTALL

    OS: Oracle Linux Server release 5.7 DB: 12.1.0.1.0 挂载镜像:mkdir /media/diskmount /dev/cdrom /media/dis ...

  6. oracle 临时表空间

    环境: OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3. ...

  7. 手动书写小代码-foreach实现机制

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

  8. Windows 10 IoT Core Samples

    Windows 10 IoT Core Samples Welcome to the Windows 10 IoT Core Samples These samples have been valid ...

  9. 为什么要用Message Queue

    摘录自博客:http://dataunion.org/9307.html?utm_source=tuicool&utm_medium=referral 为什么要用Message Queue 解 ...

  10. 条款21:必须返回对象object时,不要返回其引用reference

    如下为一个有理数类,其中包含一个用来计算乘积的函数: #include <iostream> using namespace std; class Rational { public: R ...