First, the development environment
Maven version: 3.0.5
Spring Version: spring3.2.3 release
JUnit Version: 4.11
Eclipse version: 3.7.2 R2
JDK Version: 1.6
Ii. List of documentsPom.xml
<properties><project.build.sourceencoding>utf-8</project.build.sourceencoding></ Properties><dependencies><dependency><groupid>junit</groupid><artifactid> Junit</artifactid><version>4.11</version><scope>test</scope></dependency> <dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId> <version>1.9.5</version><scope>test</scope></dependency><dependency>< Groupid>org.springframework</groupid><artifactid>spring-test</artifactid><version> 3.2.3.release</version><scope>test</scope></dependency><dependency><groupid >org.springframework</groupId><artifactId>spring-context</artifactId><version> 3.2.3.release</version></dependency></dependencies><build><plugins><plugin ><groupid>org.apache.maven.plUgins</groupid><artifactid>maven-surefire-plugin</artifactid><version>2.5</version ><configuration><skiptests>true</skiptests></configuration></plugin></ Plugins></build>
Applicationcontext.xml
<?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" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd "><bean id=" employee "name=" employee "class=" Com.tfry.spring.Employee "autowire=" Default "><constructor-arg name=" Age "value=" ></constructor-arg><constructor-arg name= "name" Value= "Zhangsan" ></constructor-arg></bean></beans>
Employee.java
public class Employee {private Integer age;private String name;public integer getage () {return age;} public void Setage (Integer age) {this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Employee (Integer age,string name) {this.age=age;this.name=name;} @Overridepublic String toString () {return "Employee name is" +name+ "," +age;}}
Iii. Main steps
1. Using unit tests provided by Springframework
Path of the package: ORG.SPRINGFRAMEWORK.TEST.CONTEXT.JUNIT4
The ability to implement unit tests by adding only two annotations
@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations={"Classpath*:applicationcontext.xml"})
@RunWith Everyone is not unfamiliar, JUNIT4 used it to do junit loader
@ContextConfiguration is used primarily to load the spring configuration file path: is an array of strings that can load multiple spring configuration files
2. Basic use
Import static Org.junit.assert.assertequals;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.ApplicationContext; Import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner, @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"Classpath:ApplicationContext.xml"}) public class Empolyeetest {@ Autowiredapplicationcontext ctx; @Testpublic void Testemployee () {Employee employee = (employee) Ctx.getbean ("employee") ); Assertequals ("Zhangsan", Employee.getname ());}}
3. Encapsulation of test base classes based on Abstractjunit4springcontexttests
Springtest.java
Import Org.junit.test;import Org.junit.runner.runwith;import org.springframework.context.ApplicationContext; Import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.abstractjunit4springcontexttests;import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner, @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations={"Classpath*:applicationcontext.xml"}) public class Springtest extends abstractjunit4springcontexttests {public <T> T getbean (class<t> type) { return Applicationcontext.getbean (type); } Public Object Getbean (String beanname) { return Applicationcontext.getbean (beanname); } Protected ApplicationContext GetContext () { return applicationcontext; }
Then the other test class simply inherits the class, eliminating the need to bind the Application object each time.
The next step is how to do unit testing in WebApp and how to combine ormapping frameworks such as Hibernate.
Finally attach the source code
Source Address
Use JUnit to unit Test spring in _01 basic applications under MAVEN