Guest User

Untitled

a guest
May 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. @Service
  2. public class KeyService {
  3.  
  4. @Cacheable("keyCache")
  5. public String getKey() {
  6. return "fakeKey";
  7. }
  8. }
  9.  
  10. @RunWith(SpringRunner.class)
  11. @SpringBootTest
  12. public class KeyServiceTest {
  13.  
  14. @Autowired
  15. private KeyService keyService;
  16.  
  17. @Test
  18. public void shouldReturnTheSameKey() {
  19.  
  20. Mockito.when(keyService.getKey()).thenReturn("key1", "key2");
  21.  
  22. String firstCall = keyService.getKey();
  23. assertEquals("key1", firstCall);
  24.  
  25. String secondCall = keyService.getKey();
  26. assertEquals("key1", secondCall);
  27. }
  28.  
  29. @EnableCaching
  30. @Configuration
  31. static class KeyServiceConfig {
  32.  
  33. @Bean
  34. KeyService keyService() {
  35. return Mockito.mock(KeyService.class);
  36. }
  37.  
  38. @Bean
  39. CacheManager cacheManager() {
  40. return new ConcurrentMapCacheManager("keyCache");
  41. }
  42. }
  43. }
  44.  
  45. @Service
  46. public class KeyService {
  47.  
  48. @Cacheable("keyCache")
  49. public String getKey(String param) {
  50. return "fakeKey";
  51. }
  52. }
  53.  
  54. @RunWith(SpringRunner.class)
  55. @SpringBootTest
  56. public class KeyServiceTest {
  57.  
  58. @Autowired
  59. private KeyService keyService;
  60.  
  61. @Test
  62. public void shouldReturnTheSameKey() {
  63.  
  64. Mockito.when(keyService.getKey(Mockito.anyString())).thenReturn("key1", "key2");
  65.  
  66. String firstCall = keyService.getKey("xyz");
  67. assertEquals("key1", firstCall);
  68.  
  69. String secondCall = keyService.getKey("xyz");
  70. assertEquals("key1", secondCall);
  71. }
  72.  
  73. @EnableCaching
  74. @Configuration
  75. static class KeyServiceConfig { //The same code as shown above }
  76. }
Add Comment
Please, Sign In to add comment