Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

477
Views
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"));   
    }    
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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());
}
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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 ;)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!