[转]maven下使用junit对spring进行单元测试_01基本应用

转自:http://my.oschina.net/dlpinghailinfeng/blog/336694
摘要  maven环境下 使用junit 对spring进行单元测试实例,亲测通过

 

一、开发环境

maven版本:3.0.5

spring版本:spring3.2.3 release

junit版本:4.11

eclipse版本:3.7.2 r2

jdk版本:1.6 

二、文件清单

pom.xml

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
< 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

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
<? 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 >

 

 

Employee.java

 

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
         
     }
 
}

 

 

三、主要步骤

 

1.使用springframework提供的单元测试

包的路径:org.springframework.test.context.junit4下

只需要加入两个注解就可以实现单元测试的功能

 

?
1
2
@RunWith (SpringJUnit4ClassRunner. class )
@ContextConfiguration (locations={ "classpath*:ApplicationContext.xml" })

 

 

@RunWith 大家并不陌生,junit4里用它来做junit加载器

 

@ContextConfiguration 主要用来加载spring的配置文件路径:是一个字符串数组,可以加载多个spring配置文件

2.基本使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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());
     }
 
}

 

 

3.封装基于AbstractJUnit4SpringContextTests的测试基类

SpringTest.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
    }

 

 

然后其他测试类只需要继承该类即可,可以省去每次都要绑定Application对象。

下一步就是在webapp中如何进行单元测试和如何结合hibernate等ORMapping框架进行单元测试。

最后附上源码

源码地址

猜你喜欢

转载自currentj.iteye.com/blog/2293987