主要步骤

1. 在工程的pom文件中增加spring-test的依赖:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>

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

新建测试类,并在该类上加上两个注解:

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

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

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

测试代码如下:

 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的测试基类

 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对象。

4. 当项目变得复杂,其中的spring配置文件被拆分成了多个,这样该如何引入多个配置文件呢?如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-ctx-service.xml", "classpath*:spring-ctx-dao.xml" })

这样就可以轻松的引入多个spring的配置文件了。或者配置符合某一个正则表达式的一类文件,如:

 @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring-ctx-*.xml")

Maven下使用Junit对Spring进行单元测试的更多相关文章

  1. 使用Junit对Spring进行单元测试实战小结

    Demo代码: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/ ...

  2. Maven下org.junit.Test无法使用

    原文地址: https://blog.csdn.net/allenChenZhiMing/article/details/81412983 我在看Spring in action(第四版)的时候,看到 ...

  3. Spring Boot 单元测试示例

    Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的单元测试. 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启 ...

  4. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)

    在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...

  5. 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(上)

    SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中spring是一个轻 ...

  6. Maven 跳过Junit单元测试

    转载自:https://blog.csdn.net/arkblue/article/details/50974957 -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至 ...

  7. Spring MVC -- 单元测试和集成测试

    测试在软件开发中的重要性不言而喻.测试的主要目的是尽早发现错误,最好是在代码开发的同时.逻辑上认为,错误发现的越早,修复的成本越低.如果在编程中发现错误,可以立即更改代码:如果软件发布后,客户发现错误 ...

  8. spring+hibernate单元测试案例

    1,maven创建web工程 2,导入相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...

  9. maven+springMVC+mybatis+junit详细搭建过程 ***

    springMVC+mybatis框架搭建 在上一遍博客中以及讲诉了新建maven项目的流程,现在紧跟上一遍文章,接着搭建spring项目 首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什 ...

随机推荐

  1. hdu 4190 Distributing Ballot Boxes 二分

    Distributing Ballot Boxes Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. 实习小结(三)--- 权限管理(RBAC)

    这一周,大多数时间 用来做需求分析,细化每个页面需要实现的功能.由于这个项目需要四种身份登录查看,分别是学生,老师,领导,管理员.每个身份登入系统显示得页面都不相同,四个角色分析完成后,统计了一下页面 ...

  3. CentOS-Linux系统下安装JDK

    一般情况下,Linux系统都有自带的JDK,但不符合开发要求,所以需要卸载,重新安装JDK 步骤1:查看现有安装的JDK版本 命令: rpm -qa | grep -i java 步骤2:卸载已有软件 ...

  4. python中类变量和实例变量

    1. 类变量和实例变量 在Python Tutorial中对于类变量和实例变量是这样描述的: Generally speaking, instance variables are for data u ...

  5. 在 Ubuntu上使用 MySQL

    MySQL 安装配置 https://help.ubuntu.com/12.04/serverguide/mysql.html MySQL Manual http://dev.mysql.com/do ...

  6. drupal的权限设置

    通过hook_menu()设置url的权限,有两种方式: 方式一:定义函数,通过 access callback 'access callback' => 'fun()', function f ...

  7. Same Tree 比较两个二叉树是否完全相同

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. 有关使用 iview 表单验证的问题

    Vue的UI解决框架,element-UI, iview-UI 有关表单验证使用的是同一个插件,async-validator,有关这个插件的用法就不做赘述,但是在iview表单的使用中可能会用到验证 ...

  9. Integer 和 int 值比较

    int 是基本数据类型,会进池,可以使用 == 判断两个值相等 Integer是对象,比较对象不能使用 == , 可以使用Integer.intValue()将取出对象值比较

  10. Spring MVC基本配置和实践(二)

    1. springmvc: 是一个表现层框架,作用是从请求中接收传入的参数,将处理后的结果数据返回给页面展示 2. ssm整合: 1)Dao层 pojo.mapper接口.mapper映射文件(使用逆 ...