Junit单元测试Spring框架接口
来源:互联网 发布:中国网络有墙保护了谁 编辑:程序博客网 时间:2024/06/06 04:00
Web项目整合Spring后,所有的接口,类都可以加载到ApplicationContext中。项目不用启动也可以加载到内存中,并通过applicationContext获取。
使用main方法测试,如下:
package testCase;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.xy.domain.CategoryDTO;import com.xy.service.ProductIndexService;public class MyTest {public static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");public static void main(String[] args) throws Exception{ProductIndexService productIndexService = (ProductIndexService)applicationContext.getBean("productIndexService");CategoryDTO categoryDTO = new CategoryDTO();categoryDTO.setCategoryName("测试类目");categoryDTO.setCategoryLevel(1);categoryDTO.setCategoryDesc("main方法测试类目");productIndexService.insertCategory(categoryDTO);}}
使用main方法有以下的不足:
1.不能使用@AutoWire自动注入获取service,只能通过getBean()方式获取。
2.调用增、删、改以后,数据都会留在表里,只能手动删除表中数据。一般来说测试不需要表里保留数据。
3.如果要测试的类里面有多个方法,用main方法测试就很不方便。
使用Junit测试以后就可以解决以上的问题。
准备:
需要下载junit的jar包,引入项目内。(Junit4.8).
引入spring相关jar包(Spring-test)。
测试基类:
spring中提供了两种支持Junit的父类。
AbstractTransactionalJUnit4SpringContextTests//需要spring配置事务AbstractJUnit4SpringContextTests//不需要事务
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean>在测试类中需要加入@RunWith和@ContextConfiguration来自动加载Spring上下文信息。
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:application-context.xml")编写测试基类:
package base;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Junit测试基类 * 继承AbstractJUnit4SpringContextTests * @author javaw * */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:application-context.xml")public class BaseTestCase extends AbstractJUnit4SpringContextTests{}
package base;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * Junit测试基类 * 继承AbstractTransactionalJUnit4SpringContextTests * 可实现事务回滚 * @author javaw * */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:application-context.xml")public abstract class BaseTransactionTestCase extends AbstractTransactionalJUnit4SpringContextTests{}
测试类:
可以使用@Autowired或者@Resource将serivce层注入。
package testCase;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import base.BaseTransactionTestCase;import com.xy.domain.CategoryDTO;import com.xy.service.ProductIndexService;public class ProductIndexTestCase extends BaseTransactionTestCase{@Autowiredpublic ProductIndexService productIndexService;@Beforepublic void setUp() throws Exception {}@Testpublic void testInsertCategory() throws Exception{CategoryDTO categoryDTO = new CategoryDTO();categoryDTO.setCategoryName("测试类目");categoryDTO.setCategoryLevel(1);categoryDTO.setCategoryDesc("Junit测试类目");int i = productIndexService.insertCategory(categoryDTO);Assert.assertEquals(1, i);}}
测试类分别继承BaseTestCase和BaseTransactionTestCase,分别执行了insert方法以后,继承BaseTestCase时,数据库中增加了一条数据。
继承BaseTranscationTestCase时,数据库中并没有插入数据。可见事务已经自动回退掉了。
0 0
- Junit单元测试Spring框架接口
- junit Spring框架 单元测试
- spring框架_03 junit单元测试
- Spring框架整合Junit单元测试框架
- spring框架学习(三)junit单元测试
- Spring框架下的Junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- spring框架学习(三)junit单元测试
- linux分区满了,如何做
- Linux常用命令-新增用户
- Linux工具快速教程
- Python奇技淫巧
- 【Leetcode】Rotate Array III
- Junit单元测试Spring框架接口
- Leetcode-159.Longest Substring with At Most Two Distinct Characters
- 数据类型简单结构
- HDOJ 1568 Fibonacci(斐波那契通项公式+取对数)
- 分级发布与灰度发布
- python3 内置函数大全
- 数据库并发控制 你选乐观锁还是悲观锁?
- 如何实现字幕效果,cocos2dx ,Lua
- LeetCode Scramble String