云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 在Spring中快速使用EHCache注解

在Spring中快速使用EHCache注解

  • 62 次阅读
  • 3 次下载
  • 2025/5/25 18:52:58

8. }

为了使测试简单一些我们需要使用了两个依赖:Spring Test 和Mockito:

1.

2. org.mockito

3. mockito-all 4. 1.8.5 5. jar

6. compile 7. 8.

9. org.springframework 10. spring-test 11. 3.0.3.RELEASE 12. jar

13. compile 14.

测试类将依托于SpringJUnit4ClassRunner运行:

1. @RunWith(SpringJUnit4ClassRunner.class)

2. @ContextConfiguration(locations = { \ }) 3. public class CachingTest { 4. @Autowired

5. ApplicationContext context; 6. @Autowired

7. CacheManager cacheManager; 8. MessageStorage storage;

9. MessageStorage storageDelegate; 10. MessageController controller; 11. @Before

12. public void before() throws Exception {

13. storageDelegate = Mockito.mock(MessageStorage.class);

14. storage = (MessageStorage) context.getBean(\); 15. storage.setDelegate(storageDelegate);

16. controller = new MessageController(storage);

17. cacheManager.clearAll(); 18. }

19. @Test

20. public void testCaching_MessagesCache() { 21. controller.getAllMessages(); 22. controller.getAllMessages();

23. verify(storageDelegate, times(1)).findAllMessages(); 24. }

25. @Test

26. public void testCaching_MessagesCacheRemove() { 27. controller.getAllMessages(); 28. controller.getAllMessages();

29. controller.addMessage(new Message()); 30. controller.getAllMessages();

31. verify(storageDelegate, times(2)).findAllMessages();

32. verify(storageDelegate, times(1)).addMessage(any(Message.class)); 33. }

34. @Test

35. public void testCaching_MessageCache() { 36. controller.getMessageById(1); 37. controller.getMessageById(1);

38. controller.addMessage(new Message()); 39. controller.getMessageById(1);

40. verify(storageDelegate, times(1)).findMessage(1);

41. verify(storageDelegate, times(1)).addMessage(any(Message.class)); 42. } 43.}

本示例是一个模拟的对象用于测试实际的MessageStorage上的调用次数。本测试Spring上下文的配置如下:

1.

2.

3. xmlns:context=\ xmlns:ehcache=\ng-annotations.googlecode.com/svn/schema/ehcache-spring\

4. xsi:schemaLocation=\://.springframework.org/schema/beans/spring-beans-3.0.xsd

5. ://.springframework.org/schema/context ://.springframework.org/schema/context/spring-context-3.0.xsd

6. ://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring ://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd\>

7.

8.

9. 10.

11.

12. 13.

14. 15.

现在我们准备运行创建好的测试用例去验证实际在MemoryMessageStorage上的调用。我们期待的结果是:

总结

使用Ehcache Spring Annotations是很简洁的,通过上述简单的一些步骤我们试图介绍如何在您的应用中使用缓存,我强烈建议您在您的项目中使用本工具之前先去我们的网站上去读一下文档,这样您会了解一些这篇文章没有覆盖到的用法。

搜索更多关于: 在Spring中快速使用EHCache注解 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

8. } 为了使测试简单一些我们需要使用了两个依赖:Spring Test 和Mockito: 1. 2. org.mockito 3. mockito-all 4. 1.8.5 5. jar 6. compile 7. 8. 9.

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com