Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock redis template

I am facing a issue in mock redis template. Can any one help me to write unit test for below class.

@Repository
public class CasheRepo {

    @Autowired
    private RedisTemplate<String, Object> template;

    public Object getObject(final String key) {
    return template.opsForValue().get(key);
    }
}

And below is unit test class. But it is not working. It shows null point exceptions

@RunWith(MockitoJUnitRunner.class)
public class CashRepoTest {
    @InjectMocks
    private CasheRepo casheRepo = new CasheRepo();

    private @Mock RedisConnection redisConnectionMock;
    private @Mock RedisConnectionFactory redisConnectionFactoryMock;

    private RedisTemplate redisTemplate;

    @Before
    public void setUp() {   Mockito.when(redisConnectionFactoryMock.getConnection()).thenReturn(redisConnectionMock);   
    redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(redisConnectionFactoryMock);
    redisTemplate.afterPropertiesSet();
    }

    @Test
    public void getObjectTest() {
    Mockito.doNothing().when(redisTemplate).opsForValue().set("spring", "data");
    redisTemplate.afterPropertiesSet();  
    System.out.println(redisTemplate.opsForValue().get("spring"));   
    }    
}
like image 370
lahirumw Avatar asked Apr 09 '17 04:04

lahirumw


4 Answers

you can mock redisTemplate like this:

@Mock
RedisTemplate<String, String> redisTemplate;

@Mock
private ValueOperations valueOperations;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    Mockito.when(redisTemplate.opsForValue()).thenReturn(valueOperations);
    Mockito.doNothing().when(valueOperations).set(anyString(), anyString());
}
like image 55
张泽光 Avatar answered Sep 22 '22 14:09

张泽光


For those who want to do the same with HashOperations get() and put()

    @Mock
    RedisTemplate<String, String> redisTemplate;
    
    @Mock
    private HashOperations hashOperations;
     
    @Test
    void getFromCache() {
         Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOperations);
         when(hashOperations.get("test-key", "test-hash-key")).thenReturn("value123");
         RedisCacheServiceImpl cacheService = new RedisCacheServiceImpl(redisTemplate);
         assertEquals("value123", cacheService.getFromCache("test-key", "test-hash-key"));
    }

Hope it helps you ;)

like image 27
Adarsh D Avatar answered Sep 20 '22 14:09

Adarsh D


While I faced with a similar task, I've made a tool(annotation) based on mock-jedis to solve it in easy way. You could read about it here: https://github.com/incu6us/redis-mock-template or just add a dependency to your project:

<dependency>
  <groupId>com.github.incu6us.redis</groupId>
  <artifactId>redis-mock-template</artifactId>
  <version>0.0.1</version>
</dependency>
like image 34
Viacheslav Pryimak Avatar answered Sep 18 '22 14:09

Viacheslav Pryimak


You are creating redisTemplate via constructor, and it was not got by DI. Try to use @Spy annotation:

@Spy
private RedisTemplate redisTemplate = new RedisTemplate();

It will allow DI to inject your instance of RedisTemplate.

like image 45
MaximSadym Avatar answered Sep 19 '22 14:09

MaximSadym