spring mvc junit测试类

问题描述:

使用junit来测试接口是否正确,于是来搞一下顺便记录下:

首先jar准备:junit.jar,spring-test-4.2.5.RELEASE.jar

然后创建一个用于测试的路径文件夹,把spring-core.xml,spring-hibernate.xml等相关文件放入到同等路径下,

创建一个用于测试的java文件,代码如下:

spring-core.xml中的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

	<!-- 启动自动扫描该包下所有注解过的java文件(例如@Controller) -->
	<context:component-scan base-package="com.wondersgroup.wegov" />
	<context:annotation-config />
	<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.wondersgroup.wegov.platform.dao.WljDeptInfoDao;
//SpringJUnit4ClassRunner继承junit包中BlockJUnit4ClassRunner以此来扩展spring测试类的功能
@RunWith(SpringJUnit4ClassRunner.class)
//spring-core.xml的作用是扫描所有添加过注解的文件,所以所有与添加注解文件有关联的文件得保证能够加载的到,spring的其他功能也是由此延伸扩展的
@ContextConfiguration("classpath:spring-core.xml")
public class Test {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Autowired
	private InfoDao infodao;

	@org.junit.Test
	@Transactional
	public void test() {
		List<Object> list = infodao.executeSQL("select s.*,decode(s.department_code,'4355','@@','4356','@@@') from table_name s");
		for (int i = 0; i < list.size(); i++) {
			Object[] o = (Object[]) list.get(i);
			for (Object object : o) {
				System.out.print(object + "\t");
			}
			System.out.println();
		}
	}

}

此代码仅供参考,若有问题,跪求指正。

发布了61 篇原创文章 · 获赞 9 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u012129030/article/details/102756257