Spring-based reusable Junit test class design

Source: Internet
Author: User

Spring-based reusable Junit test class design

We do not need to perform unit tests during SSH projects. In many cases, we do not want the unit test results to affect the real database, then we need to use transactions for management.

The JUnit Test base class is 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. depende NcyInjectionTestExecutionListener; 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 = "applicationContext. the name of the Transaction Manager in the xml file. The default value is tran. SactionManager "* defaultRollback = true whether the transaction is rolled back or committed after execution. If you do not want to leave the test data in the database, you can set it to true */@ TransactionConfiguration (transactionManager =" transactionManager ", defaultRollback = false)/*** 3. after the listener * for registering the test case is added to TransactionalTestExecutionListener, it is responsible for parsing the annotation of @ Transactional, @ NotTransactional, @ Rollback, and other Transaction Annotations * @ Transaction annotation for the test method to work in the Transaction environment, however, the transaction will be rolled back before the test method returns. You can use @ Rollback (false) * to submit the transaction before the test method returns. The @ NotTransactional annotation prevents the test method from working in the transaction environment. * After DependencyInjectionTestExecutionListener is added, @ Autowired can be identified. If the property name of this class is the same as bean * id or name in applicationContext, Autowired */@ TestExecutionListeners ({TransactionalTestExecutionListener. class, DependencyInjectionTestExecutionListener. class})/*** enables all methods of the test case to work in the transaction environment */@ Transactional/*** 4. context configuration * locations: Specify applicationContext. path of the xml file. You can use this attribute to manually specify the location of the Spring * configuration file, you can specify one or more Spring * configuration files @ ContextConfiguration (locations = {"xx/yy/beans1.xml", "xx/yy/beans2.xml"}) * inheritLocations: whether to inherit the Spring configuration file in the parent test case class. The default value is true */@ ContextConfiguration (locations = {"classpath *: ApplicationContext. xml "}) public abstract class implements acttransactionalspringcontexttest extends?acttransactionaljunit4springcontexttests {}


With this, we can use Junit without having to use the getBean method in the Ioc container of Spring to retrieve the desired Bean. You can use the following code directly in the project:

        @Resourcepublic ClassDao classDaoImpl;


However, there is another problem. We often use an entity in the dao layer and service tests. We can also abstract the entity to the parent class, as shown below:

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, only the following code is left to be written:

public class GradeDaoImplTest extends GradeTestUtil{@Resourcepublic GradeDao gradeDaoImpl;@Test@Rollbackpublic void save() throws Exception{gradeDaoImpl.add(grade);}

In addition, transaction rollback is automatically performed when a transaction is successfully run without affecting the database.















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.