Spring integrates Junit testNG
Spring provides a set of test integration interface classes specifically for Junit testNG-AbstractSpringContextTests class. For testNG, it is its subclass: AbstractTestNGSpringContextTests. After the integration of Spring and testNG, spring injection can be easily used as long as the test class inherits this class during unit testing. With seamless integration of spring and testNG, we can test the classes managed by spring IoC containers as we write normal classes (otherwise we must re-load Spring beanfactory before starting the unit test, use getBean ("xxx") to obtain classes in the IoC container .) In addition, the cache of the test class spring beanfactory allows multiple test classes to share the same beanfactory instance, which reduces repeated beanfactory generation and improves the running efficiency. Test cases that inherit this class are carried out in spring-managed transactions. After testing, the database records will not be affected. After you perform some operations on the database, it will automatically roll back the database, so that your test has no impact on the environment. The integration code is as follows @ ContextConfiguration (locations = {"classpath: spring/spring-mvc.xml "}) public class MyFirstTestNg extends AbstractTestNGSpringContextTests {} package com. bestpay. cif. product. service; import cn.com. bestpay. response; import com. bestpay. cif. core. enummodel. *; import com. bestpay. cif. product. bizparammodel. customerCreateReqDto; import com. bestpay. cif. product. bizparammodel. customerCreateResDto; import com. bestpay. cif. product. facade. customerProdFacade; import org. junit. assert; import org. springframework. beans. factory. annotation. autowired; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import org. springframework. test. context. testng. abstractTestNGSpringContextTests; import org. testng. annotations. afterClass; import org. testng. annotations. beforeClass; import org. testng. annotations. test;/*** Created by laiwenhua on 2015/10/28. * // @ ContextConfiguration (locations = "classpath: spring/spring-mvc.xml") @ ContextConfiguration (locations = {"classpath: spring/spring-mvc.xml "}) public class MyFirstTestNg extends acttestngspringcontexttests {@ Autowired private CustomerProdFacade customerService; @ BeforeClass public void beforeClass () {System. out. println (customerService + "----- personal account opening Test start ---------------") ;}@ Test public void TestNgLearn () {CustomerCreateReqDto dto = new CustomerCreateReqDto (); dto. setCustomerType (CustomerType. PERSON. getCode (); dto. setLoginName ("13600000005"); dto. setBindMobile ("13600000005"); dto. setTelecomCompany (TelecomCompany. DX. getCode (); dto. setRegChannel (CustRegChannel. STATIION. getCode (); dto. setDataSourceType (DataSourceType. NORMAL. getCode (); dto. setCertificateType (CertificateType. ID. getCode (); dto. setCertificateNo ("22222222222014111304"); dto. setCustomerName ("User 111304"); dto. setLoginType (LoginType. MOBILE. getCode (); dto. setLoginPwd ("123456"); dto. setPayPassword ("654321"); dto. setCreatedBy ("webSys"); dto. setOccupation ("IT"); dto. setGender ("F"); dto. setEmail ("[email protected]"); dto. setPostCode ("200000"); dto. setHomePhone ("66666666"); System. out. println ("-------------------" + customerService); Response <CustomerCreateResDto> res = customerService. createPersonCustomer (dto, "createPersonCustomerTest"); System. out. println ("createPersonCustomerTest return result:" + res. isSuccess () + "|" + res. getErrorCode () + "|" + res. getErrorMsg (); Assert. assertEquals (true, res. isSuccess ();} @ AfterClass public void afterClass () {System. out. println ("----- personal account opening test end ---------------");}}