简介

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. SSM配置后可以访问静态html文件但无法访问其他后台接口的解决方案

    web.xml中的一段 <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class> ...

  2. [ZJOI2013]K大数查询——整体二分

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是: 1 a b c:表示在第a个位置到第b个位置,每个位置加上一个数c 2 a b c:表示询问从第a个位置到第b个位置,第C大的数是多少. ...

  3. 大数据之路week01--自学之集合_2(Iterator迭代器)

    选代器:是遍历集合的一种方式.迭代器是依赖于集合而存在的.我有一个集合: Collection c = new ArrayList();我们给集合中添加元素: c. add("hello' ...

  4. git 合并代码

    分支 dev 及衍生分支 dev-ctj 一.rebase 1.git checkout dev-ctj 2.git rebase -i head~num[num 是本分支的提交数,多个提交数先合并为 ...

  5. 构建大型 Vue.js 项目的10条建议

    下面是我在开发大型 Vue 项目时的最佳实践.这些技巧将帮助你开发更高效.更易于维护和共享的代码. 今年做自由职业的时候,我有机会开发了一些大型 Vue 应用程序.我所说的这些项目,Vuex stor ...

  6. 多线程之美1一volatile

    目录 一.java内存模型 1.1.抽象结构图 1.2.概念介绍 二.volatile详解 2.1.概念 2.2.保证内存可见性 2.3.不保证原子性 2.4.有序性 一.java内存模型 1.1.抽 ...

  7. 201871010114-李岩松《面向对象程序设计(java)》第一周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  8. suseoj The wheat of the prime minister

    1202: 2018四川理工学院大学生ACM程序设计:The wheat of the prime minister 时间限制: 1 Sec  内存限制: 128 MB提交: 4  解决: 3[提交] ...

  9. 【SpringBoot | Redis】SpringBoot整合Redis

    SpringBoot整合Redis 1. pom.xml中引入Redis相关包 请注意,这里我们排除了lettuce驱动,采用了jedis驱动 <!-- redis的依赖 --> < ...

  10. 【SSM】自定义属性配置的使用

    首先,建立xxx.properties 文件在resource文件夹中,此处我们自定义的配置文件是oj-config.properties 然后,在applicationContext.xml中注册这 ...