简介

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中不开启注解扫描

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
  2. xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"      
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans       
  4. https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
  5. https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop
  6. https://www.springframework.org/schema/aop/spring-aop.xsd">   
  7. <bean id="userService" class="com.igeek.service.impl.UserServiceImpl"></bean>
  8. </beans>

service层:

  1. public class UserServiceImpl implements IUserService {
  2. @Override   
  3. public void save() { 
  4. System.out.println("save...");   
  5. }
  6. }

测试类:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml")
  3. public class Test01 { 
  4. @Autowired   
  5. private IUserService userService;
  6. @Test   
  7. public void test01(){ 
  8. userService.save();   
  9. }
  10. }

2.第二种:在applicationContext.xml中开启注解扫描

配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
  3. xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"      
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans       
  5. https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
  6. https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop
  7. https://www.springframework.org/schema/aop/spring-aop.xsd">  
  8. <!--开启注解扫描-->   
  9. <context:component-scan base-package="com.igeek"></context:component-scan>
  10. </beans>

service层:

  1. @Service("userService")
  2. public class UserServiceImpl implements IUserService {
  3. @Override   
  4. public void save() { 
  5. System.out.println("save...");   
  6. }
  7. }

测试类:

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml")
  3. public class Test01 { 
  4. @Autowired   
  5. private IUserService userService;
  6. @Test   
  7. public void test01(){ 
  8. userService.save();   
  9. }
  10. }

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. [Luogu5384][Cnoi2019] 雪松果树

    传送门 虽然这题是一道二合一,也不算难,但还是学到了很多东西啊,\(k\) 级儿子个数的五种求法!!我还是觉得四种比较好( \(k\) 级儿子个数有五种求法,你知道么? --鲁迅 首先 \(k\) 级 ...

  2. [考试反思]0825NOIP模拟测试31:喘息

    好吧,我又活了 大脸又. 240,220,210,200,200... T1是个不会证明的傻子找规律算上看了一遍三道题之后一共20分钟搞定. skyh打的是错的可是成功qj全部测试点得到AC(会被手模 ...

  3. NOIP 模拟22

    这次考试真的是像教练说的真的挺难的,但是人家rank1还是100+, 但是咕咕蛊!

  4. 分享一份关于Hadoop2.2.0集群环境搭建文档

    目录 一,准备环境 三,克隆VM 四,搭建集群 五,Hadoop启动与测试 六,安装过程中遇到的问题及其解决方案 一,准备环境 PC基本配置如下: 处理器:Intel(R) Core(TM) i5-3 ...

  5. Redis必备面试题《基础篇》

    Date:2019-11-12 读前思考: 面试官会问什么样的问题? 所问的问题背后真实的套路是什么? 喜欢问Redis哪些问题? 如何顺畅回答面试问的问题?吊打面试官. 1.什么是Redis? Re ...

  6. Git III: 撤销操作

    所谓撤销操作,无非就是后悔药.对Git来说,撤销操作主要是以下几块: 撤销已经提交的Commit. 对已经通过git add加入Stage的文件,进行unstage操作. 对已经是Untracked却 ...

  7. C++中对C的扩展学习新增语法——函数重载

    函数重载 1.函数重载语法 1.同一个作用域(全局作用域.命名空间作用域.类作用域) 2.参数个数不同 3.参数类型不同 4.参数顺序不同 代码实现: 当函数名字一样的时候,通过参数类型.参数个数.参 ...

  8. 在VMware15.5中安装CentOS7_7_64bit

    一.创建虚拟机 在我的另一个随笔里有. 地址为:https://www.cnblogs.com/qi-yuan/p/11692092.html 只是在虚拟机安装操作系统时候选择 Linux 而不是 W ...

  9. nyoj 274-正三角形的外接圆面积 (R = PI * a * a / 3)

    274-正三角形的外接圆面积 内存限制:64MB 时间限制:1000ms 特判: No 通过数:14 提交数:22 难度:0 题目描述: 给你正三角形的边长,pi=3.1415926 ,求正三角形的外 ...

  10. 【Java】面向对象之多态

    生活中,比如动物中跑的动作,小猫.小狗和大象,跑起来是不一样的.再比如飞的动作,昆虫.鸟类和飞机,飞起来也是不一样的.可见,同一类的事物通过不同的实际对象可以体现出来的不同的形态.多态,描述的就是这样 ...