如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试。

> 基本的搭建

(1)引入所需的包

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>

(2)写测试类

测试类中要设置加载哪些Spring的配置(我这里是“/config/application*.xml”),然后就可以注入容器中的bean了。

package com.nicchagil.mybatis3spring3intg.junit;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.nicchagil.mybatis3spring3intg.bean.User;
import com.nicchagil.mybatis3spring3intg.service.UserService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/config/application*.xml"})
public class JunitTest { @Autowired
private UserService userService; @Test
public void c1() {
List<User> userList = userService.query(new User());
System.out.println(userList);
} }

> 常见的用法

常用的方式是将加载配置的部分公用出来:

package com.nicchagil.mybatis3spring3intg.junit;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/config/application*.xml"})
public class BaseJunit { }

然后需要的各个测试类继承公用类:

package com.nicchagil.mybatis3spring3intg.junit;

import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import com.nicchagil.mybatis3spring3intg.bean.User;
import com.nicchagil.mybatis3spring3intg.service.UserService; public class UserServiceTest extends BaseJunit { @Autowired
private UserService userService; @Test
public void c1() {
List<User> userList = userService.query(new User());
System.out.println(userList);
} }

【Spring】Junit加载Spring容器作单元测试的更多相关文章

  1. 【Spring】Junit加载Spring容器作单元测试(整理)

    [Spring]Junit加载Spring容器作单元测试 阅读目录 >引入相关Jar包 > 配置文件加载方式 > 原始的用法 > 常见的用法 > 引入相关Jar包 一.均 ...

  2. Junit加载Spring容器作单元测试

    阅读目录 > 基本的搭建 > 常见的用法 如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单 ...

  3. spring boot 加载web容器tomcat流程源码分析

    spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...

  4. junit测试用例加载spring配置文件

    junit加载pom引用项目的xml配置文件,如果定义了<beans profile="dev">,必须在测试用例类上面加上标记 @ActiveProfiles(&qu ...

  5. 加载spring容器

    import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...

  6. Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)

    最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...

  7. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  8. Spring中加载配置文件的方式

    原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...

  9. Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)

    http://www.cnblogs.com/goody9807/p/4227296.html(很不错啊) 容器先加载spring的xml,然后初始化bean时,会为bean赋值,包括里面的占位符

随机推荐

  1. matplotlib 安装与使用

    1.在ubuntu下输入 sudo apt-get install python-matplotlib 安装matplotlib 2.简单代码使用

  2. 网站部署后Parser Error Message: Could not load type 的解决方案

    asp.net 的Webproject 项目是在64bit机上开发,默认选项发布后,部署到32bit的服务器上,出现Parser Error Message: Could not load type的 ...

  3. C# 代码笔记

    一.使循环不卡 Application.DoEvents(); System.Threading.Thread.Sleep(5); 二.计算代码运行时间 Stopwatch sw = new Stop ...

  4. Python脚本模拟登录网页之CSDN篇

    1. 通过Firefox配合插件Tamper Date获取登录时客户端向服务器端提交的数据, 并且发现lt和execution这两个字段每次登录时都不一样. POSTDATA=username=you ...

  5. dedecms 使用

    初看dedecms的后台界面就是一头雾水.不懂的词语多,什么模型,什么栏目,什么频道,不懂.相比于wordpress的分类category,标签tag,文章post,页面page而言,织梦后台难懂. ...

  6. P1074 靶形数独 - 40

    #include <bits/stdc++.h> #define searchnext(x, y) y == 9 ? search(x + 1, 1) : search(x, y + 1) ...

  7. Ignoring Extra Elements in mongoDB C# Driver

    MongoDB删除字段后会报错: Element ... does not match any field or property of class Customer. 需要在实体类增加 [BsonI ...

  8. 关于JavaScript的判断语句(1)

    if语句: if( 判断条件 ){ 判断结果为true执行语句: } if...else语句: if(判断条件){ 判断结果为true时执行的语句: }else{ 判断结果为false时执行语句: } ...

  9. ES6 module export options 模块导出、导入语法

    http://stackoverflow.com/questions/25494365/es6-module-export-options A year and some later, here is ...

  10. 以一则LUA实例说明敏捷开发中“分离构造和使用”原则

    分离构造和使用 构造含义是功能的实现, 此功能是一个定义明确的处理过程, 开放出明确的接口给调用者使用. 则使用者可以直接调用接口进行使用, 但是使用者需要搞清楚, 那些是构造, 那些是使用. 不要再 ...