简介

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();   
}
}

最新文章

  1. Unity3D 脚本编译器无法关联VisualStudio2012解决办法
  2. iOS8: Ignore manifest download, already have bundleID
  3. DirectX.Capture Namespace
  4. [转载]python gevent
  5. Mac与iPhone屏幕录制
  6. 常用js总结1
  7. js按Enter键提交表单
  8. IOS 杂笔-5(NSTimer极浅析)
  9. UI-程序的运行顺序
  10. POI 读取Excel文档中的数据——兼容Excel2003和Excel2007
  11. php通过文件头检测文件类型通用类(zip,rar…)(转)
  12. paip.输入法编程----一级汉字1000个
  13. pdf点击超链接后返回:alt+ 向左 /向右
  14. ecshop2.73修改密码方法|ecshop2.73修改密码方法
  15. Windows共享设置
  16. C++ Primer Plus学习:第七章
  17. hand first python 选读(2)
  18. JMS学习之理论基础
  19. python之MySQL学习——防止SQL注入(参数化处理)
  20. NFS4 挂载同主机多个目录

热门文章

  1. JavaSE常用API
  2. PHP、 Ruby、Python、Java、C++、C、Objective C——编程语言之禅
  3. NOIP模拟测试14
  4. js基础总结03 --操作数组
  5. day 2 DP专场
  6. 大数据之路day02_1--运算符
  7. SSM整合案例--用户登录
  8. Zabbix日志监控插件
  9. PHP 核心特性 - 匿名函数
  10. ffmpeg 编译安装