maven下使用junit对spring进行单元测试_01基本应用
来源:互联网 发布:翻倍关注法优化 编辑:程序博客网 时间:2024/05/19 22:00
https://my.oschina.net/dlpinghailinfeng/blog/336694
一、开发环境
maven版本:3.0.5
spring版本:spring3.2.3 release
junit版本:4.11
eclipse版本:3.7.2 r2
jdk版本:1.6
二、文件清单
pom.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="20"></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 is"+age;}}
三、主要步骤
1.使用springframework提供的单元测试
包的路径:org.springframework.test.context.junit4下
只需要加入两个注解就可以实现单元测试的功能
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})
@ContextConfiguration 主要用来加载spring的配置文件路径:是一个字符串数组,可以加载多个spring配置文件
2.基本使用
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.封装基于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; }
下一步就是在webapp中如何进行单元测试和如何结合hibernate等ORMapping框架进行单元测试。
最后附上源码
源码地址
0 0
- maven下使用junit对spring进行单元测试_01基本应用
- maven下使用junit对spring进行单元测试_01基本应用
- maven下使用 junit对 spring进行单元测试
- 使用Junit对Android应用进行单元测试
- 使用Junit对Android应用进行单元测试
- 使用Junit对Android应用进行单元测试
- Android:使用junit对应用进行单元测试
- 使用Junit对Android应用进行单元测试
- 使用Junit对Android应用进行单元测试
- junit对spring进行单元测试
- Junit 对应用进行单元测试
- Spring 使用Junit 进行单元测试
- 在Spring应用中使用junit进行单元测试
- 在spring下使用junit做单元测试的基本方法
- Junit对Spring Controller进行单元测试
- 对应用进行单元测试(junit)
- Maven工程使用Junit进行单元测试
- 使用 JUnit 对规则进行单元测试
- JavaScript中的Promise机制
- c#多维数组
- Nginx实战(三) 日志配置与切割
- HDU1232 并查集<并>
- [生存志] 第64节 君子与小人
- maven下使用junit对spring进行单元测试_01基本应用
- 迪杰斯特拉求最短路径
- iOS 【Xcode使用代码设置各式各样的字体】
- MFC线程同步那些事
- HDU 1879 继续畅通工程(Kruskal算法)
- Spring AOP 实现原理
- 【Hibernate框架学习】:Hibernate进阶之详解Hibernate配置文件和对象关系映射配置文件
- 51nod 1105 第K大的数【二分+思维】好题~
- 求3×3矩阵对角线元素之和---2种方法