阅读目录

如果我们需要对我们的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);
} }

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

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

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

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

    如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 & ...

  3. 加载spring容器

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

  4. web.xml中配置——加载spring容器

    <context-param> <param-name>contextConfigLocation</param-name> <param-value> ...

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

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

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

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

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

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

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

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

  9. struts加载spring

    为了在Struts中加载Spring context,需要在struts-config.xml文件中加入如下部分: <struts-config> <plug-in classNam ...

随机推荐

  1. EMR的fair-scheduler.xml

    cat /etc/ecm/hadoop-conf/fair-scheduler.xml <?xml version="1.0"?> <allocations> ...

  2. Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  3. [转帖]从零开始入门 K8s:应用编排与管理:Job & DaemonSet

    从零开始入门 K8s:应用编排与管理:Job & DaemonSet https://www.infoq.cn/article/KceOuuS7somCYbfuykRG 陈显鹭 阅读数:193 ...

  4. SQLSever--基础学习--创建登录用户&创建数据库用户&分配权限

    如题,本文简记一下SQL Sever里面登录用户(login)的创建,数据库用户(DBUser)的创建,以及给数据库用户分配权限(Grant). 数据库有三层保护机制: 第一层:登录用户以及登录密码的 ...

  5. Power BI学习

    常见用途: 1.连接数据 2.转换和清洗该数据,以创建数据模型 3.创建视觉对象,如提供数据的可视化表示形式的图表或图形 4.在一个或者多个报表页上创建作为视觉对象集合的报表 5.使用Power BI ...

  6. LOJ2557 CTSC2018组合数问题(提交答案)

    直接利用simulator退火应该可以得到大量分数. op=1:1,4,5,6,10 即构造序列{ai},最小化Σti,ai+rai,aj. 1:暴搜/退火. 4:观察到图大致成一条链(注意其中有两个 ...

  7. 开发人员需求能kill杀死其它阻塞自己的会话,测试发现需要alter system权限有风险

    模拟开发人员需求,可以杀死其它阻塞自己的会话1.能有查询阻塞会话确认的权限SQL> grant select on v_$session to testa;SQL> grant selec ...

  8. ( 十二)Json的使用(上)

    一.什么是JSON 1.1 javaScript中定义对象的几种方式 使用内置对象 自定义对象构造 使用JSON符号  (1)使用内置对象 JavaScript可用的内置对象可分为两种: 1,Java ...

  9. PL/SQL Developer的错误提示弹框的文本显示乱码问题

    问题:Windows中文环境下,PL/SQL Developer的错误提示弹框文本为乱码,如下: 解决过程:1.使用SELECT * FROM v$nls_parameters;查询得知服务器的字符集 ...

  10. 微信小微商户申请入驻 .NET C#实现微信小微商户进件API

    微信小微商户申请入驻 .NET C#实现微信小微商户进件API官方小微商户专属接口文档 微信支付SDK 微信支付官方SDK与DEMO下载 图片上传 图片上传接口API文档 证书下载 证书下载接口API ...