加载spring的单元测试

本文介绍了如何在单元测试中加载Spring的bean。通过引入spring-test和junit的测试依赖,我们可以利用Spring的测试工具对应用程序进行有效测试。

有时候我们需要加载spring的bean进行单元测试,代码如下:

/* 
 * Copyright (c) 2017, S.F. Express Inc. All rights reserved.
 */
package com.sf.ddt.redisUtils;

import java.io.Serializable;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sf.framework.cacheproxy.ICache;
import com.sf.framework.cacheproxy.redis.RedisCache;

/**
 * 描述:
 * 
 * 
HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年3月23日      835089         Create
 * ****************************************************************************
 * 
* @author 835089 * @since 1.0 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/spring/application-common.xml") public class redisUtilTest { @Resource private ICache redisCache; @Test public void setKey(){ System.out.println("ddt common"); //redisCache.set("javaTest", "test java"); Object obj =redisCache.get("javaTest"); System.out.println(obj+""); } }


依赖jar

<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>


<!-- 测试工具 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值