Usually we do not have to do the SSH project to do unit testing, and many times, we do not want the results of unit testing on the real database has an impact, then we need to use the transaction to manage.
The JUnit test base classes are as follows:
Package Com.sms.test.base;import Org.junit.runner.runwith;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.testexecutionlisteners;import Org.springframework.test.context.junit4.abstracttransactionaljunit4springcontexttests;import Org.springframework.test.context.junit4.springjunit4classrunner;import Org.springframework.test.context.support.dependencyinjectiontestexecutionlistener;import Org.springframework.test.context.transaction.transactionconfiguration;import Org.springframework.test.context.transaction.transactionaltestexecutionlistener;import org.springframework.transaction.annotation.transactional;/** * 1. Specify the test Case runner */@RunWith ( Springjunit4classrunner.class)/** * 2. Transaction configuration * transactionmanager= " Here is the name of the transaction manager in the Applicationcontext.xml file, the default is TransactionManager "* defaultrollback=true the transaction is rolled back or committed, and if you do not want the test to be left in the database, Can be set to True */@TransactionConfiguration (TransactionManager = "TransactionManager", Defaultrollback = False)/** * 3. Register a test Case listener * Join TRAfter Ansactionaltestexecutionlistener, it is responsible for parsing annotations of @Transactional, @NotTransactional, and @Rollback, etc. * @Transaction Annotations let the test method work in a transactional environment, but the transaction is rolled back before the test method returns. You can use @Rollback (FALSE) * To have the test method return before the transaction is committed. @NotTransactional annotations Let the test method not work in a transactional environment. * After adding Dependencyinjectiontestexecutionlistener, you can recognize @autowired, the property name of this class is the same as the Bean * ID or name in ApplicationContext. Then autowired */@TestExecutionListeners ({transactionaltestexecutionlistener.class, Dependencyinjectiontestexecutionlistener.class})/** * All methods of the test case will work in a transactional environment */@Transactional/** * 4. Context configuration * Locations: Specifies the path to the Applicationcontext.xml file that allows you to manually specify the location of the spring * configuration file, specifying one or more Spring * configuration Files @contextconfiguration ( locations={"Xx/yy/beans1.xml", "Xx/yy/beans2.xml"}) * Inheritlocations: If you want to inherit the spring configuration file from the parent test case class, the default is True */@ Contextconfiguration (locations = {"Classpath*:applicationcontext.xml"}) public abstract class Abstracttransactionalspringcontexttest extendsabstracttransactionaljunit4springcontexttests {}
With this we can use JUnit and not use the Getbean method from spring's IOC container to get the bean we want. You can use the following code directly as a project:
@Resourcepublic Classdao Classdaoimpl;
However, there is also a problem, we are in the DAO layer and service testing is often used by an entity, then we can also abstract entities to the parent class, as follows:
public class Gradetestutil extends Abstracttransactionalspringcontexttest{public gradeentity grade;public Gradetestutil () {grade = new gradeentity (); Grade.setcode (1); Grade.setgradename ("Gradename"); Grade.setremark (" Remark ");}}
At the end of the test there is only one point of code to write:
public class Gradedaoimpltest extends gradetestutil{@Resourcepublic Gradedao gradedaoimpl; @Test @rollbackpublic void Save () throws Exception{gradedaoimpl.add (grade);}
And the transaction rollback occurs automatically when the operation is successful and does not have an impact on the database
Design of the reusable JUnit test class based on spring