Spring Transaction Management JUnit rollback __java_servlet

Source: Internet
Author: User
Tags rollback

From: http://blog.csdn.net/funi16/article/details/8691575

When you write unit tests, it is generally the operation of the database to check, this time, if the previous deletion of a record, the nature of the subsequent program will not find this record, so you can configure the spring transaction Management or test framework to roll back and forth, reduce the workload. The database used is PostgreSQL and MySQL.

In writing this article, many places borrowed from the following two articles:

Http://www.cnblogs.com/rainisic/archive/2012/01/22/Spring_Test_Framework.html

http://blog.163.com/wf_shunqiziran/blog/static/17630720920121084325322/ 1, creating a test class

Create a test class with the name "tested class name + test" recommended.

Test class should inherit with Abstractjunit4springcontexttests or abstracttransactionaljunit4springcontexttests

For the selection of Abstractjunit4springcontexttests and Abstracttransactionaljunit4springcontexttests classes:

If you need to use transaction management in your test class (for example, to roll back test content after the test results come out), you can use the Abstracttransactionaljunit4springtests class. The use of transaction management is the same as the normal use of spring transaction management. It should be noted that if you want to use declarative transaction management, that is, using the Abstracttransactionaljunitspringcontexttests class, Please include the TransactionManager bean in the Applicationcontext.xml file:

<bean id= "TransactionManager"
class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

If you do not add the above bean, nosuchbeandefinitionexception is thrown, indicating that no bean named ' TransactionManager ' is definded.
2. Configure Test class

Add the following to configure the location of the Applicationcontext.xml file before class.

@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations = "Classpath:applicationContext.xml")
3. Create test methods

Create a test method with the name "method name + test" recommended.

Add @Test 4 above the test method and execute through JUnit 4

Right-click Method name, select "Run as" → "JUnit Test"

I used to use this method to configure the automatic rollback of unit testing, but in the new project found no, so it is a variety of Niang Google, finally found. By the way, I'm searching for a central reason why I can't rollback automatically:

1, the code contains Try-catch block can not be rolled back. (personal feeling is not reliable)

2, the method has a Non-public method.

3, Loaded 2 times the configuration file, resulting in the insert and rollback method is not a thing inside. That's the kind of situation I've come across.

For example: I have already loaded a applicationcontext.xml file in the annotation, and loaded again in the setup (), resulting in the method being submitted as 2 things. So I use annotations to load the bean. [Java] view plain copy @Autowired private Appointmentdao Appointmentdao;

Unit test Cases The note above is: [Java] view plain copy @RunWith (springjunit4classrunner.class) @TransactionConfiguration ( TransactionManager = "Txmanager", Defaultrollback = True) @Transactional/** read the configuration file to the running environment. Note: file's path/@ContextConfiguration (locations={"Classpath:applicationContext.xml"})

In Applicationcontext.xml file: [Java] view plain copy <bean id= "Txmanager class=" Org.springframewor       K.jdbc.datasource.datasourcetransactionmanager "> <property name=" DataSource "ref=" DataSource "/>" </bean>

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.