捣鼓了差不多一天。。。终于把"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. Js学习笔记一(鼠标事件.....)

    1.encodeURI与decodeURI()转化url为有效的url(能被识别) Url="http://news.baidu.com/p.php?id='测试'&姓名=hkui& ...

  2. sublime配置问题

    sublime本身功能有限,我们需要装上一些插件使其变得强大.sublime在各个操作系统下都可以运行,但在linux下运行需要注意中文输入的问题. 下面我主要介绍一下常用插件.配置的建议以及在lin ...

  3. (转)Android属性设置android:noHistory="true"

    设置 android:noHistory="true"后,该Activity在statck中不留历史痕迹.默认的值是false. 举例说明,假设有三个Activity分别是:A,B ...

  4. CentOS6.4安装VNC

    http://jingyan.baidu.com/article/ca2d939dd1dabbeb6c31ce24.html 一.安装 VNC 默认情况下,CentOS 6.4 是没有安装的. 检查是 ...

  5. Sorl之.net操作

    http://www.cnblogs.com/zhangweizhong/category/771055.html 插入: SolrNet.Startup.Init<Movie>(&quo ...

  6. C语言实现冒泡排序法和选择排序法代码参考

    为了易用,我编写排序函数,这和直接在主调函数中用是差不多的. 我认为选择排序法更好理解!请注意 i 和 j ,在写代码时别弄错了,不然很难找到错误! 冒泡排序法 void sort(int * ar, ...

  7. Android---WebView显示Html乱码问题

    webView.loadData(result,"text/html","UTF-8"); 反正是用上面的方法无法解决乱码的问题. 使用下面的方法就能完美解决了 ...

  8. 九度oj 1523 从上往下打印二叉树

    原题链接:http://ac.jobdu.com/problem.php?pid=1523 建树,再层次遍历bfs.为了找根方便些,加了father指针... 如下: #include<algo ...

  9. Go append方法

    append用来将元素添加到切片末尾并返回结果.看代码: package main import "fmt" func main() { x := [],,} y := [],,} ...

  10. random_names随机名字生成

    // 先从txt文件中获取姓和名数组 - (void)getNames{ NSString *resourcePath1 = [[NSBundle mainBundle] pathForResourc ...