spring测试用例的编写

本文详细介绍两种JUnit测试写法,一种使用SpringJUnit4ClassRunner结合spring-ehcache配置,另一种通过ClassPathXmlApplicationContext初始化CacheManager。两种方法均演示了如何进行缓存操作,包括存取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

junit测试写法有两种

  1. 第一种:

              

package cn.njhq.shiroTest;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:spring-ehcache.xml")
public class Test2 {

    @Autowired
	private CacheManager cacheManager;
	
	
	@org.junit.Test
	public void test(){
        //获取根据名称缓存对象
		Cache cache = cacheManager.getCache("authorizationCache");
        //存入对象
		cache.put("name", "chenmin");
        //取出对象
		ValueWrapper valueWrapper = cache.get("name");
		String name = valueWrapper.get().toString();
		System.out.println(name);
	}
}
  1. 第二种:
public class Test {

	private CacheManager cacheManager;
	@Before
	public void init() throws Exception{
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-ehcache.xml");
		cacheManager = applicationContext.getBean(EhCacheCacheManager.class);
	}
	
	@org.junit.Test
	public void test(){
		Cache cache = cacheManager.getCache("authorizationCache");
		cache.put("name", "chenmin");
		ValueWrapper valueWrapper = cache.get("name");
		String name = valueWrapper.get().toString();
		System.out.println(name);
	}
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值