junit測試寫法有兩種
- 第一種:
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);
}
}
- 第二種:
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);
}
}