In the same test, compared with Spring TestContext, JUnit has the following advantages:
1. Spring TestContext can be used to manually set test transaction rollback,No data destruction
2. You do not need to manually assemble beans in the test class. You only need one @ Autowired to automatically assemble beans.
---------------- Split line ---------------------------
This document describes how to use the TestContext test framework for web project and java project.
This section summarizes how to use the Spring TestContext testing framework:
1. Build a testing framework Environment
Make sure that your project is a web project so you can introduce the WEB-INF package in the WebRoot/spring-test.jar/lib package.
(Jar package location: I am using spring-framework-2.5.5 and have this spring-test.jar under/dist/modules in this folder)
Make sure that your JUnit version is better than 4.5. Otherwise, JUnit4.5 or lower may not be supported.
2. Use TestContext to test the framework
The applicationContext. xml file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans -2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <! -- Annotation declaration --> <context: annotation-config/> <context: component-scan base-package = "com. huohuo"/> <! -- Transaction declaration --> <tx: annotation-driven transaction-manager = "txManager"/> <! -- Configure the dataSource data source --> <bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "> <value> com. mysql. jdbc. driver </value> </property> <property name = "url"> <value> jdbc: mysql: // localhost: 3306/spring </value> </property> <property name = "username"> <value> root </value> </property> <property name = "password"> <value> </property> </bean> <! -- Configure sessionFactory --> <bean id = "sessionFactory" class = "org. springframework. orm. hibernate3.annotation. annotationSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "/> <property name =" annotatedClasses "> <list> <value> com. huohuo. domain. user </value> <value> com. huohuo. domain. log </value> </list> </property> <property name = "hibernateProperties"> <props> <prop key = "hibernate. dialect "> org. hi Bernate. dialect. mySQLDialect </prop> <prop key = "hibernate. show_ SQL "> true </prop> <prop key =" hbm2ddl. auto "> update </prop> </props> </property> </bean> <! -- Configure transaction TXManager --> <bean id = "txManager" class = "org. springframework. orm. hibernate3.HibernateTransactionManager "> <property name =" sessionFactory "ref =" sessionFactory "/> </bean> </beans>
The userServiceTest class code is as follows:
Package com. huohuo. test; import org. junit. test; import org. junit. runner. runWith; import org. springframework. beans. factory. annotation. autowired; import org. springframework. test. annotation. rollback; import org. springframework. test. context. contextConfiguration; import org. springframework. test. context. junit4.SpringJUnit4ClassRunner; import org. springframework. test. context. transaction. transactionConfiguration; import org. springframework. transaction. annotation. transactional; import com. huohuo. service. userService; @ RunWith (SpringJUnit4ClassRunner. class) @ ContextConfiguration (locations = "/applicationContext. xml ") // Spring configuration file location @ TransactionConfiguration (transactionManager =" txManager ") // the id of my transaction bean is txManager
// Important: if this line is not added, the transaction bean named transactionManager will be automatically searched. this is not the transaction bean set in xml, so you need to change it to the id name of your transaction bean.
@ Transactional public class UserServiceTest {
@ Autowired // automatically assemble the bean, or write it as @ Resource (name = "userService") private UserService userService; @ Rollback (false) // set the transaction to not be rolled back, if this parameter is not added, the system automatically rolls back @ Test public void test () {userService. saveUserAndLog ();}}
Summary
If you want to perform unit tests in the Spring environment, you can perform the following Configuration:
- Continue to use the Junit4 testing framework, including its @ Test annotation tag and the definition of related classes and methods.
- Use @ RunWith (SpringJUnit4ClassRunner. class) to start Spring's support for the test class.
- You need to use the @ ContextConfiguration annotation label to specify the location of the Spring configuration file or configuration class.
- You must use @ Transactional to enable automatic transaction management.
- You can use @ Autowired to automatically weave Spring beans for testing.
In addition, you do not need:
- Manually load the Spring configuration file
- Manual cleaning of database changes
- Obtain application context manually and then bean instance
------------- Split line -------------
The following describes how to use the TestContext test framework for common java classes.
1. Introduce the spring-test.jar package (with instructions above, or go to baidu to download)
2. The test class must inheritExtendsAbstractTransactionalJUnit4SpringContextTests class
3. TestContext is used in the remaining test classes, which is the same as the preceding web project.
The java test code in the java project is as follows:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="/applicationContext.xml") @TransactionConfiguration(transactionManager="txManager")@Transactional public class UserServiceTest extends AbstractTransactionalJUnit4SpringContextTests { @Rollback(false) @Test public void test() { ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService us =(UserService)ct.getBean("userService"); us.saveUserAndLog(); }