Spring Junit集成测试

例子如下:

package com.junge.demo.spring;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
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 com.junge.demo.spring.service.IPerface;
import com.junge.demo.spring.service.ServiceConfig;

/**
 * Unit test for simple App.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=ServiceConfig.class)
public class AppTest {
    @Autowired
    private List<IPerface> perfaceList;

    @Test
    public void addTest() {
        System.out.println("333");
    }

    @Test
    public void playTest() {
        assertEquals(2, perfaceList.size());
    }
}

其中用到了2个注解

[email protected] 这是Junit提供的,注意Junit版本要和spring兼容,例如spring用4.1.13.RELEASE版本,Junt可以用4.12版本,如果不兼容,用例跑不起来。

[email protected] 这是spring提供的注解,用来配置spring的上下文配置,如果是使用javaconfig风格配置的,如上面例子所示;如果是使用xml文件配置的,可以用下面的配置:

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

一般在项目中测试时,会配置一个基类测试类,其他测试类都继承自该基类,这样就不需要在每一个测试类中添加配置。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public abstract class BaseServiceTest {

}

原文地址:https://www.cnblogs.com/junge8618/p/8365423.html

时间: 2024-08-30 12:26:32

Spring Junit集成测试的相关文章

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

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

spring junit 做单元测试,报 Failed to load ApplicationContext 错误

spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locations = { "classpath:/spring/applicationContext.xml","classpath:/spring/app-config.xml", …… 改成 @ContextConfiguration(locations = { "c

spring junit jpa transaction

package com.fengshu.gotian.applicationImpl; import java.util.List; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration;

spring+junit单元测试

<1>读取文件: 配置文件在classes下:locations = {"classpath*:/spring/applicationContext.xml"} 配置文件在web-inf下:locations = {"file:web/WEB-INF/applicationContext.xml"} <2>实例类: package com.test; import java.util.List; import org.junit.Test;i

idea下maven项目下spring junit 测试用例

使用idea在编写的类下右键Go->Test或者ctrl+shift+t,点击create new test会在相应目录下创建test类 别写代码如下 @RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:/config/**/applicationContext*.xml" }, loader = GenericXmlContextLoader

spring junit

转载自 http://blog.csdn.net/funi16/article/details/8691575 在写单元测试的时候,一般是对数据库进行增删改查的操作,这个时候,如果之前删除了某条记录,自然后面的程序就找不到这条记录了,所以可以通过配置spring的事务管理或者测试框架来回滚,减少工作量.使用的数据库是postgreSQL和mysql. 在写这篇文章的时候,很多地方借鉴了下面两篇文章: http://www.cnblogs.com/rainisic/archive/2012/01/

maven+spring+junit测试要注意的事情

使用maven方式创建webapp工程的资料网上一大堆,在这里也不详细说了.在创建完成之后,里面说到要转动态web工程时要切换为3.0版本,但是我本地切换不了,网上的方法好像也没用,暂时也没用到这块.等SSM框架搭建好了之后再细说这块.今天要说的是之前按照网上一个教程搭建SSM框架时遇到的针对spring针对junit单元测试的坑,因为涉及到maven的版本管理,一直没跳出来.昨天下午给解决了.有可能别的eclipse或者其他环境不会遇到这问题,但这里我还是要提醒一下.按照以下教程https:/

Spring Junit 读取WEB-INF下的配置文件

假设Spring配置文件为applicationContext.xml 一.Spring配置文件在类路径下面 在Spring的java应用程序中,一般我们的Spring的配置文件都是放在放在类路径下面(也即编译后会进入到classes目录下). 以下是我的项目,因为是用maven管理的,所以配置文件都放在“src/main/resources”目录下 这时候,在代码中可以通过 [java] view plaincopy ApplicationContext applicationContext 

Solved:Spring Junit Test NoSuchMethodError

最近在看Spring in action这本书,在Ubuntu上配好了环境开始开发,没想到做了第二章的第一个例子就遇到了一个错误. 首先我在src/main/java文件夹下的controller包内建了一个test类CDPlayerTest,然后提示无法解析ContextConfiguration和SpringJUnit4ClassRunner.根据IDEA提示,我选择了add library org.springframework to classpath. 然后一切看起来都很顺利,心情舒畅