maven版本:3.0.5 java
spring版本:spring3.2.3 release web
junit版本:4.11 spring
eclipse版本:3.7.2 r2 apache
jdk版本:1.6 數組
<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>
<?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.xsd http://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="20"></constructor-arg> <constructor-arg name="name" value="zhangsan"></constructor-arg> </bean> </beans>
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; } @Override public String toString(){ return "Employee name is "+name+",age is"+age; } }
包的路徑:org.springframework.test.context.junit4下 app
只須要加入兩個註解就能夠實現單元測試的功能 框架
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})
@ContextConfiguration 主要用來加載spring的配置文件路徑:是一個字符串數組,能夠加載多個spring配置文件 eclipse
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 { @Autowired ApplicationContext ctx; @Test public void testEmployee(){ Employee employee =(Employee) ctx.getBean("employee"); assertEquals("zhangsan",employee.getName()); } }
SpringTest.java webapp
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; }
下一步就是在webapp中如何進行單元測試和如何結合hibernate等ORMapping框架進行單元測試。 maven
最後附上源碼