junit4 spring集成测试

很多时候我看见小伙伴这样测试spring的service或者dao,每个类后面写个main,使用new方式加载spring配置文件,获取需要测试的实例,然后对实例进行测试。稍微好一点的在junit测试类里面new加载spring配置文件进行测试。

其实junit测试spring可以很方便的进行。这里会用到spring-test-xxx.jar,junit4的jar。

其中要注意的是:

@RunWith(SpringJUnit4ClassRunner.class)

1、如果spring配置文件applicationContext.xml在classpath路径下,即通常的src目录下,这样加载配置文件,用classpath前缀。

@ContextConfiguration(locations = { "classpath:applicationContext.xml" })

2、但是在web项目中,有些人喜欢把spring配置文件applicationContext.xml放在WEB-INF目录下,这里不是classpath目录。这种情况可以按如下方式配置:用file前缀,指定配置文件的绝对路径。貌似这种方式不是很友好。

@ContextConfiguration(locations = { "file:F:\\workspace\\web-test\\src\\main\\resources\\"
		+ "applicationContext.xml" })

spring配置文件applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd  ">

	<context:component-scan base-package="com.wss.lsl.junit.demo" />

</beans>

待测试的service,就一个方法,求两个整数的和。

package com.wss.lsl.junit.demo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class CalculateService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 计算:返回两个整数的和
	 * 
	 * @param first
	 * @param second
	 * @return
	 */
	public int sum(int first, int second) {

		logger.info("求和参数:first={}, second={}", first, second);

		return first + second;
	}
}

测试基类,所有其他的测试类集成此类,从而无需再重复配置加载spring配置文件。

package com.wss.lsl.junit.demo;

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(locations = { "classpath:applicationContext.xml" })
public class BaseTest {

	@Test
	public void _() {
	}
}

测试类如下,我们可以很方便把要测试的实例@Autowired进来,这样优雅多了。

package com.wss.lsl.junit.demo.service;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.wss.lsl.junit.demo.BaseTest;

public class CalculateServiceTest extends BaseTest {

	@Autowired
	private CalculateService calculateService;

	@Test
	public void testSum() {
		int expected = 5, first = 3, second = 2;
		int real = calculateService.sum(first, second);

		// 验证
		assertEquals(expected, real);
	}

}
时间: 2024-08-08 17:56:58

junit4 spring集成测试的相关文章

Activit单元i测试(与spring集成测试)

1.测试 eclipse下安装activiti插件以及maven 右键新建activiti project(这时会自动创建pom依赖以及activiti.cfg.xml,但还不是maven项目) 选中项目根目录右键选择configure  下的convert maven project(将项目转换成maven项目) 测试文件存放原则(不是必须):测试文件类放在test/java下面,流程bpmn放在resource下面(保证编译后都在classpath下) 命名规则:测试类和bpmn文件在同一个

使用Spring注解方式注入properties文件内容,并配合Junit4+Spring做单元测试

先看看工作目录,然后再来讲解 1.建立config.properties,我的config.properties内容如下: author_name=luolin project_info=该项目主要是用于写一些demo 2.配置Spring配置文件,读取properties文件,并设置编码格式.大家从我的项目结构图中可以看到我用了两个Spring的配置文件,其实在spring-context.xml中没有配置其他内容,只是配置扫描com.eya.property这个包,大家可能会有疑问为何包的扫

spring+xml集成测试(准备数据和验证项的外部文件化)

Spring的集成测试 单位测试和集成测试,我想大家都做过,一般情况下,一般逻辑且不需要操作数据库的情况比较适合于单位测试了.而对于一个数据库应用来说,集成测试可能比单元测试更重要,你可以想象,一个互联网应用,不是增修数据,就是查询数据了,那么验证操作在数据记录上的影响就更为需要.如果在的应用中使用了spring,那么就可以利用spring集成测试框架了,其实它所做的工作就是加载配置文件并配置其中相关的信息,其中也包括数据源等,其次在验证完成之后,回滚对数据表的操纵,当然你也可以手动的设置为不回

Spring 实践 -拾遗

Spring 实践 Junit集成 前面多次用到@RunWith与@ContextConfiguration,在测试类添加这两个注解,程序就会自动加载Spring配置并初始化Spring容器,方便Junit与Spring集成测试.使用这个功能需要在pom.xml中添加如下依赖: pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</art

12341432

1.迭代器(Iterator) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的Iterator功能比较简单,并且只能单向移动: (1) 使用方法iterator()要求容器返回一个Iterator.第一次调用Iterator的next()方法时,它返回序列的第一个元素.注意:iterator()方法是java.lang.Iterable接口,被Collection继承. (

SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结

一 开发环境 1.动态web工程 2.部分依赖 java代码: hibernate-release-4.1.0.Final.zip hibernate-validator-4.2.0.Final.jar spring-framework-3.1.1.RELEASE-with-docs.zip proxool-0.9.1.jar log4j 1.2.16 slf4j -1.6.1 mysql-connector-java-5.1.10.jar hamcrest 1.3.0RC2 ehcache 2

我的闪存备份

闪存中记录了一些技术备忘,在此记录一下: >>>> 终于将所有旧系统的控件都升级到了Delphi 2010 ! [2010-01-18 10:33:55]>>>> 测试python3.2,要升级的话有许多代码要修改. [2011-03-17 09:31:09]>>>> 今天我的me525怎么也无法上网了,用hiapn等打开也没有用,后来一查,是数据传输(设置-数据管理.数据传输-数据已启用)关掉了...唉,大家注意 [2011-05-

SpringBoot实战

SpringBoot精要 SpringBoot的四个核心 1.自动配置:针对很多Spring应用程序常见的应用功能,SpringBoot能自动提供相关配置. 在任何Spring应用程序的源代码中,都可以找到java配置或XML配置,他们为应用程序开启了特定的特性和功能. 2.起步依赖:告诉SpringBoot需要什么功能,他就能引入需要的库. SpringBoot起步依赖基本都以spring-boot-starter打头,随后是直接代表其功能的名字,比如web.test. 3.命令行界面:这是S

Spring与junit4集成测试

一.应用场景:普通java web集成spring test, 项目结构:使用的是加入jar的方式,不是maven加入依赖的方式 注意:如果是引入jar包的方式,例如:spring-test4.0.5.jar 和junit4.12.jar的方式, 如果是使用的是junit4.12.jar的版本,要加入 hamcrest-core1.3.jar包,否则报如下错误: java.lang.Exception: No tests found matching [{ExactMatcher:fDispla