Spring Test 整合 JUnit 4 使用总结

1. 加入依赖包

1<dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <version>4.9</version> 5 <scope>test</scope> 6 </dependency> 7<dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-test</artifactId> 10 <version> 3.2.4.RELEASE </version> 11 <scope>provided</scope> 12 </dependency>

2. 创建测试源目录和包

3. 创建测试类

  创建一个测试用的类,推荐名称为 “被测试类名称 + Test”。

测试类应该继承与

 AbstractJUnit4SpringContextTests 或 AbstractTransactionalJUnit4SpringContextTests

需要用到事务管理(比如要在测试结果出来之后回滚测试内容),就可以使用AbstractTransactionalJUnit4SpringTests类。事务管理的使用方法和正常使用Spring事务管理是一样的。再此需要注意的是,如果想要使用声明式事务管理,即使用AbstractTransactionalJUnitSpringContextTests类,请在applicationContext.xml文件中加入transactionManager bean:

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean>

  如果没有添加上述bean,将会抛出NoSuchBeanDefinitionException,指明 No bean named 'transactionManager' is definded.

4. 配置测试类

  添加如下内容在class前,用于配置applicationContext.xml文件的位置。

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml")

需要把applicationContext.xml和其相关的文件拉到resources下

5. 创建测试方法

  创建测试用方法,推荐名称为 “被测方法名称+ Test”。

  测试方法上方加入 @Test

6. 通过JUnit 4 执行

  右键方法名,选择则“Run As”→“JUnit Test”即可

附录:整体测试类文件

1import com.zhaogang.mapper.NoticeMapper; 2import org.junit.Test; 3import org.junit.runner.RunWith; 4import org.slf4j.Logger; 5import org.slf4j.LoggerFactory; 6import org.springframework.test.context.ContextConfiguration; 7import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; 8import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 10import javax.annotation.Resource; 11 12/** 13 * Created by weixiang.wu on 2017/8/2. 14 */ 15@RunWith(SpringJUnit4ClassRunner.class) 16@ContextConfiguration(locations = "classpath:applicationContext.xml") 17public class NoticeTest extends AbstractJUnit4SpringContextTests { 18 19 private static final Logger logger = LoggerFactory.getLogger(NoticeTest.class); 20 21 @Resource 22 private NoticeMapper noticeMapper; 23 24 25 @Test 26 public void testUser() { 27 logger.info("公共标题测试: "+noticeMapper.selectTitleByPrimaryKey(23L)); 28 } 29 30}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Spring Test 整合 JUnit 4 使用总结 - HelloWorld