Spring JUNIT4 Test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.