简介

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测试集成的更多相关文章

  1. Struts2+Spring+Mybatis+Junit 测试

    Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis  package com.action.kioskmoni ...

  2. Spring整合junit测试

    本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器, ...

  3. 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)

    我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclips ...

  4. Spring与Junit测试整合

    一.引入spring测试包:text包 二.@RunWith:指定spring对junit提供的一个运行器 @ContextConfiguration:  locations指定spring配置文件位 ...

  5. Spring项目JUnit测试报错ClassNotFoundException解决

    Eclipse项目上有红色感叹号,各包显示正常.用JUnit测试部分能运行,部分报错,报错如下: Class not found UserTestjava.lang.ClassNotFoundExce ...

  6. spring使用JUnit测试,@Autowired无法注入原因

    在测试类上加入配置文件 代码如下 @RunWith(SpringJUnit4ClassRunner.class)// SpringJUnit支持,由此引入Spring-Test框架支持!  @Cont ...

  7. 08Spring_Spring和junit测试集成

    第一步: 在项目导入 spring-test-3.2.0.RELEASE.jar 第二步: 编写测试类

  8. Spring集成JUnit测试

    1.程序中有Junit环境2.导入一个jar包.spring与junit整合jar包 spring-test-3.2.0.RELEASE.jar3.测试代码 @RunWith(SpringJUnit4 ...

  9. springboot集成junit测试与javamail测试遇到的问题

    1.springboot如何集成junit测试? 导入junit的jar包 使用下面注解: @RunWith()关于这个的解释看下这两篇文章: http://www.imooc.com/qadetai ...

随机推荐

  1. TCP/IP协议第一卷第一章

    1.链路层 链路层有时也称作数据链路层或网络接口层,通常包括操作系统中的设备驱动程序和计算机中对应的网络接口卡.它们一起处理与电缆(或其他任何传输媒介)的物理接口细节.把链路层地址和网络层地址联系起来 ...

  2. 学习笔记之vim的使用

    很多刚学习linux编程的人总是对vim有一种恐惧,我自己就是这么回事的. 可是当你努力的去尝试学习使用后,才发现它的精髓所在. 在我看来,让vim变得好用的前提是要安装两个插件,ctags和tagl ...

  3. [转载]1.3 UiPath变量的介绍和使用

    一.变量 变量主要用于存储数据,它在RPA中扮演重要的数据传递角色,是RPA编程不可或缺的一部分.它包括变量名称和变量的值,变量的值支持多种数据类型,包括从通用值,文本,数字,数据表,时间和日期,Ui ...

  4. SysTick系统定时器

    1.SysTick定时器介绍 SysTick定时器也叫SysTick滴答定时器,它是Cortex-M3内核的一个 外设,被嵌入在 NVIC 中.它是一个24 位向下递减的定时器,每计数一 次所需时间为 ...

  5. PHP更新用户微信信息的方法

    PHP更新用户微信信息的方法 大家都知道 授权登录一次 获取后 再登录就会提示已经授权登录 就没办法重新获得用户信息了 这个时候根据openid来获取了请求user/info这个获取ps:必须关注过公 ...

  6. Linux命令实战(四)

    1.Linux上的文件管理类命令都有哪些,其常用的使用方法及相关示例演示. 文件或目录的新建 touch :将每个文件的访问时间和修改时间修改为当前时间.若文件不存在将会创建为空文件,除非使用-c或- ...

  7. python快速获取网页标准表格内容

    from html_table_parser import HTMLTableParser def tableParse(value): p = HTMLTableParser() p.feed(va ...

  8. nyoj 1022 合纵连横 (并查集<节点删除>)

    合纵连横 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...

  9. SQLite性能 - 它不是内存数据库,不要对IN-MEMORY望文生意。

    SQLite创建的数据库有一种模式IN-MEMORY,但是它并不表示SQLite就成了一个内存数据库.IN-MEMORY模式可以简单地理解为,本来创建的数据库文件是基于磁盘的,现在整个文件使用内存空间 ...

  10. LeetCode Bash练习

    195. Tenth Line #!/bin/bash i= cat file.txt | while read line do #echo $line ] then echo $line fi le ...