title: Mock redis template
body: <p>I am facing a issue in mock redis template.
  Can any one help me to write unit test for below class.</p>
  
  <pre><code>@Repository
  public class CasheRepo {
  
      @Autowired
      private RedisTemplate&lt;String, Object&gt; template;
  
      public Object getObject(final String key) {
      return template.opsForValue().get(key);
      }
  }
  </code></pre>
  
  <p>And below is unit test class. But it is not working. It shows null point exceptions</p>
  
  <pre><code>@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"));   
      }    
  }
  </code></pre>