您的位置:首页 > 编程语言 > Java开发

springmvc项目单元测试方法

2016-05-03 00:00 369 查看
要完成springmvc的单元测试,需要引入springmvc-test组件

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>

编写测试类,与普通单元测试不同的是测试springmvc项目要加载相应的springmvc的装配配置文件

@RunWith(SpringJUnit4ClassRunner.class)  //加载springmvc单元测试类
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml",   //加载springmvc的装配配置文件,可以根据测试内容选择加载
"file:src/main/webapp/WEB-INF/applicationContext-MyBatis.xml")

完整测试类

package com.xxx.manager.privilege.service.impl;
import javax.annotation.Resource;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
/**
* 公共参数测试类
* @title
* @usage
* @copyright Copyright 2014 hjb365. All rights reserved.
* @author gaoxueyan
* @version $Id: ParamServiceImplTest.java, v1.0
* @create 2014年9月22日 下午1:17:49
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml",
"file:src/main/webapp/WEB-INF/applicationContext-MyBatis.xml",
"file:src/main/webapp/WEB-INF/applicationContext-SpringMVC.xml",
"file:src/main/webapp/WEB-INF/applicationContext-Third.xml"})
@TransactionConfiguration
@Transactional
public class ParamServiceImplTest {
@Resource
private ParamService paramService;
/**
* 测试方法
* @throws Exception
*/
@Test
@Rollback
public void testMethod() throws Exception{
//代码部分与junit相同
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  springmvc 单元测试