spring測試用例的編寫

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);
	}
	
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章