To do a junit test with a transaction rollback with spring TestContext

Source: Internet
Author: User

The JUnit Unit test framework is common but can disrupt database locality and a large number of hard-coded occurrences (Applicationcontext.getbean (""));

Today, Buddy, let's introduce some annotations in spring test, a spring-brought jar package, to implement unit testing + transaction rollback + annotation Dependency Injection

There are not many tutorials on the web. I mean, not much. Basic are similar to the API documentation that gives you a bunch of annotations and classes that tell you what they do. You don't know where to start.
I've been thinking about it for a few days (dude isn't very tall) and I wrote a test class test to share with you.

First import the relevant jar package and library
Jar Package Spring-test This jar under Spring's liband then import the JUNIT4 library. I don't need to teach it.

Then a new SRC folder in the project is named Test
Create a package under the test folder tentatively creating a class generic class for Com.test
I'm named SchoolTest

This schooltest class to inherit abstracttransactionaljunit4springtestcontexttests brother is purely back down after all, after all, when the translation of English still can not back down also doesn't matter actually very good understanding
Transactional a few other words on behalf of the business don't really need to explain. You know how to write a few more times.

Next add an annotation to this class @runwith (Springjunit4classrunner.class)
Runwith is the annotation of junit4 meaning that this class of parameters of this annotation is dominated by the class of the unit test parameter type is class
Springjunit4classrunner.class is a class action in springtest similar to the drive meaning that the unit test is dominated by spring test
then write a second note on this class.
@ContextConfiguration (locations={"Classpath:applicationContext.xml"})
Contextconfiguration's role is to locate your context profile, which is to reduce hard-coded getbean and the like.
Once you start testing the Spring test framework, go to the specified location to read the configuration file complete Dependency Injection
The parameter is locations Note that s represents the value of the complex argument as a string array, so the parameter is added {} But if only one of the arguments can be omitted {} but note that {} can be omitted only if the parameter type of the annotation is an array and there is only one argument
don't give me a string[in ordinary Java class] a= "SB";


and if Applicationcontext.xml is in the root directory, you must write classpath: Otherwise, it will go wrong .
As for the default Web-inf, I haven't tried it, I'm not going to write locations I don't want to try .
and then write a third note .
@TestExecutionListeners (listeners={
Dependencyinjectiontestexecutionlistener.class,
Transactionaltestexecutionlistener.class
})
Testexecutionlistener as the name implies test execution listener configuration is a series of listeners as to the role of the listener this article does not mention its own Baidu
parameter is listeners the same as the parameter type is the class array inside is a variety of listeners I picked two.
one is whether the dependency injection in the Dependencyinjectiontestexecutionlistener monitoring test class is correct or not, and I don't know what the result is, because I write the right thing every time ...
The other is a transaction in the Transactionaltestexecutionlistener listener test class if a transaction is involved in a test class, it must be equipped with this listener

Next, write a fourth note.
@TransactionConfiguration (transactionmanager= "Txmanager", Defaultrollback=true)
Transactionconfiguration as the name implies is the annotation of the configuration transaction condition
The first parameter, TransactionManager, is the ID of the bean for the transaction manager you defined in Applicationcontext.xml or bean.xml
Defaultrollback is a key parameter that indicates whether the transaction will roll out after the test is complete the parameter is Boolean and the default is true but it is strongly recommended to write true
This is my spring configuration file Most of the configuration is my handwriting is a small part of the official spring document copy

The following defines a business layer object in a class or a property member variable as you

this schoolbiz says,@Resource represents a bean that is called schoolbiz from a configuration file if you do not use the spring managed unit test this write cannot be injected because there is not a container to initialize the bean in the configuration file when Tomcat starts, Tomcat reads the configuration file and initializes But unit tests are independent of the Web container, so it's hard to code with JUnit but with spring test avoids hard coding
Write a method to test plus two annotations below
One is @test. This doesn't say much. JUnit's own annotations indicate the method to test
The other is that @transactional indicates that this method is the starting point of a transaction
Note that it is best to not use annotations to host transactions at the business level because every method has to be so much more cumbersome to use xml+spring expressions to host transactions for all methods

And finally, the logic in the method is written.

Start testing Now
Test success! Open the database nothing because spring rolled back for you. Transaction protection of the database site!
Look at the database table, there's nothing.







To do a junit test with a transaction rollback with spring TestContext

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.