Spring Cache Unit Test

Our typical business service with a cacheable method, as shown below:

@Service
public class AccountService{
	@Autowired
	public AccountDao accountDao;
	
	@Cacheable(value = "accounts")
	public Account getAccount(int accountId) {
		return accountDao.getObjectById(accountId);
	}
}
Primary concerns for this kind of service method unit test.
Concern 1: combine SpringJUnit4ClassRunner and Mockito
Solution: For common scenarios, we can apply the standard solution:

@Mock
	private AccountDao dao;

	@InjectMocks
	@Autowired
	private AccountService accountService;
	
	@Before
	public void setup() throws Exception {
		MockitoAnnotations.initMocks(this);
	}
Concern 2: Mockito and Spring proxies, the critical part is our method annotated with @Cacheable, then totally a different story now.
Solution: As we applied @Cacheable annotation(another annotations like @Transactional has same effect), Spring would generate a proxy automatically, thus direct @InjectMocks @Autowired does not work. Reference 1 taught me the following approach and it works.

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring.xml" })
@ActiveProfiles("test")
public class AccountCacheTest {

	@Mock
	private AccountDao dao;

	@InjectMocks
	@Autowired
	private AccountService accountService;

	@Before
	public void setup() throws Exception {
		MockitoAnnotations.initMocks(this);
		AccountService bas = (AccountService) unwrapProxy(accountService);
		ReflectionTestUtils.setField(bas, "accountDao", dao);
	}

	@Test
	public void testAccoutCache() throws Exception {
		/* Arrange. */
		Account acc1 = new Account();
		acc1.setId(1);

		when(dao.getObjectById(1)).thenReturn(acc1, acc1);

		/* Act. */
		accountService.getAccount(1);
		accountService.getAccount(1);

		/* Assert. */
		verify(dao, times(1)).getObjectById(1);
	}

	public static final Object unwrapProxy(Object bean) throws Exception {
		/*
		 * If the given object is a proxy, set the return value as the object
		 * being proxied, otherwise return the given object.
		 */
		if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
			Advised advised = (Advised) bean;
			bean = advised.getTargetSource().getTarget();
		}
		return bean;
	}
}

LESSON: When test cache, we'd better utilize the assertSame.


Reference

1. http://kim.saabye-pedersen.org/2012/12/mockito-and-spring-proxies.html

2. http://www.captaindebug.com/2012/09/spring-31-caching-and-cacheable.html#.UmDsiflHK8s

    http://www.captaindebug.com/2012/09/spring-31-caching-and-cacheevict.html#.UmDhNflHLQI




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值