For almost a day ... Finally, the "No Session found for the current thread" problem solved
Environment: Spring 4.0.6 RELEASE + Hibernate 4.2.2 Final
The toss record is as follows:
1. "No Session found for the current thread", find the configuration file in the Sessionfactory configuration, confirm error;
2. Check the written test cases and try to modify the annotations: @Transactional and @transactionconfiguration, not resolved;
3. Re-check the DAO layer code and corresponding entity, confirm no problem;
4. Search "No Session found for current thread", some say it is necessary to add <prop key= "Hibernate.current_session_context_class" in the configuration file > Thread</prop>
Tried, the result did not have "no Session found for the current thread", but appeared "Hibernateexception:contains is not valid without active transaction", Indicates that there is no transaction and that the error is greater.
5. Then search, find the following blogs:
http://www.iteye.com/topic/1126047
According to the above blog content, plus service layer code, and test pass, depressed ...
http://blog.csdn.net/funi16/article/details/8691575
After seeing this blog, oh a sound, decisively put extends abstractjunit4springcontexttests replaced extends abstracttransactionaljunit4springcontexttests , this will add the transaction management, you can also roll back!
@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= "Classpath:applicationContext_persistence.xml") @TransactionConfiguration (TransactionManager= "TransactionManager", Defaultrollback =true) Public classActiondaoimpltestextends abstracttransactionaljunit4springcontexttests {@AutowiredPrivateActionservice Actionserviceimpl; @AutowiredPrivateActiondao Actiondaoimpl; @Test//@Rollback Public voidTestadd ()throwsException {Action action=NewAction (); Action.setlogindate (NewDate ()); Thread.Sleep (2000); Action.setlogoffdate (NewDate ()); 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 voidTESTADD2 ()throwsException {Action action=NewAction (); Action.setlogindate (NewDate ()); Thread.Sleep (2000); Action.setlogoffdate (NewDate ()); 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