Spring之junit测试集成
简介
Spring提供spring-test-5.2.1.RELEASE.jar 可以整合junit。
优势:可以简化测试代码(不需要手动创建上下文,即手动创建spring容器)
使用spring和junit集成的步骤
1.导入jar包

2.创建包com.igeek.test,创建类SpringTest
通过@RunWith注解,使用junit整合spring
通过@ContextConfiguration注解,指定spring容器的位置
3.通过@Autowired注解,注入需要测试的对象
在这里注意两点:
将测试对象注入到测试用例中
测试用例不需要配置<context:component-scan base-package="com.igeek"/></context:component-scan>,因为使用测试类运行的时候,会自动启动注解的支持(仅对该测试类启用)
举例说明一下
1.第一种:在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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.igeek.service.impl.UserServiceImpl"></bean>
</beans>
service层:
public class UserServiceImpl implements IUserService {
@Override
public void save() {
System.out.println("save...");
}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test01 {
@Autowired
private IUserService userService;
@Test
public void test01(){
userService.save();
}
}
2.第二种:在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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.igeek"></context:component-scan>
</beans>
service层:
@Service("userService")
public class UserServiceImpl implements IUserService {
@Override
public void save() {
System.out.println("save...");
}
}
测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test01 {
@Autowired
private IUserService userService;
@Test
public void test01(){
userService.save();
}
}
Spring之junit测试集成的更多相关文章
- Struts2+Spring+Mybatis+Junit 测试
Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis package com.action.kioskmoni ...
- Spring整合junit测试
本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...
- 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)
我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclips ...
- Spring与Junit测试整合
一.引入spring测试包:text包 二.@RunWith:指定spring对junit提供的一个运行器 @ContextConfiguration: locations指定spring配置文件位 ...
- Spring项目JUnit测试报错ClassNotFoundException解决
Eclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found UserTestjava.lang.ClassNotFoundExce ...
- spring使用JUnit测试,@Autowired无法注入原因
在测试类上加入配置文件 代码如下 @RunWith(SpringJUnit4ClassRunner.class)// SpringJUnit支持,由此引入Spring-Test框架支持! @Cont ...
- 08Spring_Spring和junit测试集成
第一步: 在项目导入 spring-test-3.2.0.RELEASE.jar 第二步: 编写测试类
- Spring集成JUnit测试
1.程序中有Junit环境2.导入一个jar包.spring与junit整合jar包 spring-test-3.2.0.RELEASE.jar3.测试代码 @RunWith(SpringJUnit4 ...
- springboot集成junit测试与javamail测试遇到的问题
1.springboot如何集成junit测试? 导入junit的jar包 使用下面注解: @RunWith()关于这个的解释看下这两篇文章: http://www.imooc.com/qadetai ...
随机推荐
- *args和**kwargs的作用
∗args的作用: *的作用有2个 打包参数(pack)和拆分参数(unpack) 函数接受实参时,按顺序分配给函数形参,如果遇到带∗的形参,那么就把还未分配出去的实参以元组形式打包(pack),分配 ...
- NOIP模拟 31
补坑 skyh又AK 赛时榜搜索我的姓: 下一条 ... 自闭了. (只是表达对B哥强烈的崇敬) (如果B哥介意我把名字贴出来请联系我删掉) T1一打眼,好像就一个gcd 康了眼大样例,觉得没啥问题 ...
- curl太复杂难用记不住?来试试Httpie一个简单的现代化命令行Http客户端
HTTPie 是一个简单的现代化命令行 HTTP 客户端. 交互友好,JSON支持,语法高亮,类wget下载,支持拓展等 功能特性 自然而且简单的命令语句 格式化且高亮显示输出内容 内置 JSON 支 ...
- [网络]HTTP
HTTP HTTP 简介 HTTP 协议是 Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web)服务器传输超文本到本 ...
- MySQL 执行计划详解
我们经常使用 MySQL 的执行计划来查看 SQL 语句的执行效率,接下来分析执行计划的各个显示内容. EXPLAIN SELECT * FROM users WHERE id IN (SELECT ...
- Deepin 下 使用 Rider 开发 .NET Core
Deepin 下 使用 Rider 开发 .NET Core 国产的 Deepin 不错,安利一下. Deepin 用了也有一两年,也只是玩玩,没用在开发上面.后来 Win10 不太清真了,就想着能不 ...
- Convolutional Sequence to Sequence Learning 论文笔记
目录 简介 模型结构 Position Embeddings GLU or GRU Convolutional Block Structure Multi-step Attention Normali ...
- JavaScript中BOM与DOM的使用
BOM: 概念:Browser Object Model 浏览器对象模型 将浏览器的各个组成部分封装成对象. 组成: Window:窗口对象 Navigator:浏览器对象 Screen:显示器屏幕对 ...
- 【科创人·独家】MegaEase左耳朵耗子陈皓复盘创业:第一年盈利被当骗子,线下广阔天地大有可为
[科创人·独家]MegaEase左耳朵耗子陈皓复盘创业:第一年盈利被当骗子,线下广阔天地大有可为 原创: babayage CTO科创圈 与上百位科技创业者共同关注科创人的成长心路. 文末有彩蛋:& ...
- go-micro+php+consul简单的微服实现
首先我们用go-micro构建一个服务.(关于go-micro的使用可以参照官方实例或者文档) //新建一个微服务 micro new --type "srv" user-srv ...