一:加入jar包

<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

二:写测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-*.xml")
public class SSMTest {
@Test
public void test(){
System.out.println("aa");
}
}

解释下用到的注解:
@RunWith:用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境
@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring/buyer/applicationContext-service.xml"}) 
导入配置文件,这里我的applicationContext配置文件是根据模块来分类的。如果有多个模块就引入多个“applicationContext-service.xml”文件。如果所有的都是写在“applicationContext.xml”中则这样导入: 
@ContextConfiguration(locations = "classpath:applicationContext.xml") 
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库! 
@Transactional:这个非常关键,如果不加入这个注解配置,事务控制就会完全失效!

spring中试用junit4测试的更多相关文章

  1. Spring项目使用Junit4测试配置

    我们经常要写junit测试,在不启动整个web项目的情况下,测试自己的service实现或者是dao实现,我们来充分利用下junit4的强大功能. 1.junit4的测试类 import java.u ...

  2. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  3. 使用JUnit4测试Spring

    测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...

  4. Junit4测试Spring

    使用Junit4.4测试 在类上的配置Annotation  @RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境  @ContextCon ...

  5. 在Spring Boot项目中使用Spock测试框架

    本文首发于个人网站:在Spring Boot项目中使用Spock测试框架 Spock框架是基于Groovy语言的测试框架,Groovy与Java具备良好的互操作性,因此可以在Spring Boot项目 ...

  6. Spring中的AOP

    什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...

  7. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  8. JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换

    本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...

  9. Spring中AOP简介与切面编程的使用

    Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...

随机推荐

  1. JavaScript中JSON的序列化和解析

    1.序列化:JSON对象----->JSON格式字符串 ①方法: JSON.stringify() ②示例程序: var jsonString = JSON.stringify(obj); al ...

  2. echars 柱状图正常状态 --》二次封装

    <template> <!-- 柱状图 正常 1. 调用页面引入 import EcharsColumnNormal from '@/components/echarsColumnN ...

  3. 根据对象属性查找对象或者数组(根据对象属性查找某数组内符合该条件的对象,数组内对象属性check为true的对象,存放到数组内) 滚动轴样式

      1.根据对象属性查找某数组内符合该条件的对象. optionComwords:[ {optionName:"名称1", optionCode: '1'}, {optionNam ...

  4. 【Python基础】_2 Python基本语法与常识(迭代优化中...)

    2 Python的基本语法 为了保证Python解释器能顺利编译所编写的代码,也为了程序员对自己和别人所编写的程序易于阅读.维护,对编程语言的语法做一些基本约定是非常必要的. 2.1 编程方式 2.1 ...

  5. urllib库认证,代理,cookie

    认证,代理,cookie 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, buil ...

  6. 使用CyclicBarrier+线程池,按总页数分批次开多线程执行逻辑

    通过CyclicBarrier+线程池的方式,同步的方式分页分批次并发高效处理逻辑,将总页数分成多个批次并发执行每页逻辑,每个批次处理DO_MAX_SIZE个页,每个批次等待DO_MAX_SIZE个页 ...

  7. JDBC插入中文数据出现?号地解决问题

    1. 查看jdbc配置是否指定编码 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/te ...

  8. 洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心)

    洛谷P1484 种树&洛谷P3620 [APIO/CTSC 2007]数据备份 题解(堆+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/132 ...

  9. 字符串hash 模板

    typedef long long ll; typedef unsigned long long ull; #define maxn 1005 struct My_Hash { ull ; ull p ...

  10. java复习(6)String、StringBuffer以及StringBuilder

    0.常见的编码表 ASC||:美国标准信息交换码,用一个字节的7位可以表示. ISO8859-1:拉丁码表.欧洲码表,用一个字节的8位来表示.无法存储汉字,或者只取了汉字的一半使用 GB2312:中文 ...