title: Redis unit test using redis mock
body: <p>I'm using the <a href="https://dzone.com/articles/java-redis-mock" rel="nofollow noreferrer">following</a> tool to run an embedded redis for unit testing.</p>
  
  <p>In the beginning of my <strong>registrationService</strong> is creating a new instance of redis server.</p>
  
  <pre><code>@Import({RedisConfiguration.class})
  @Service
  public class RegistrationService
  
  RedisTemplate redisTemplate = new RedisTemplate();  //&lt;- new instance
  
  public String SubmitApplicationOverview(String OverviewRequest) throws IOException {
  
      . . .
  
      HashMap&lt;String,Object&gt; applicationData = mapper.readValue(OverviewRequest,new TypeReference&lt;Map&lt;String,Object&gt;&gt;(){});
  
      redisTemplate.setHashKeySerializer(new StringRedisSerializer());
      redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
  
      UUID Key = UUID.randomUUID();
  
      redisTemplate.opsForHash().putAll(Key.toString(), (applicationData)); //&lt;-- ERRORS HERE
  
      System.out.println("Application saved:" + OverviewRequest);
  
      return Key.toString();
   }
  }
  </code></pre>
  
  <p>And I'm starting a mock redis server in my <strong>Test</strong> below.</p>
  
  <pre><code>...
  
  RedisServer redisServer;
  
  @Autowired
  RegistrationService RegistrationService;
  
  @Before
  public void Setup() {
      redisServer = RedisServer.newRedisServer();
      redisServer.start();
  }
  
  @Test
  public void testSubmitApplicationOverview() {
      String body = "{\n" +
              "    \"VehicleCategory\": \"CAR\",\n" +
              "    \"EmailAddress\": \"email@email.com\"\n" +
              "}";
      String result = RegistrationService.SubmitApplicationOverview(body);
  
      Assert.assertEquals("something", result);
  }
  </code></pre>
  
  <p>Redis settings in <strong>application.properties</strong></p>
  
  <pre><code>#Redis Settings
  spring.redis.cluster.nodes=slave0:6379,slave1:6379
  spring.redis.url= redis://jx-staging-redis-ha-master-svc.jx-staging:6379
  spring.redis.sentinel.master=mymaster
  spring.redis.sentinel.nodes=10.40.2.126:26379,10.40.1.65:26379
  spring.redis.database=2
  </code></pre>
  
  <p>However, I'm getting a <code>java.lang.NullPointerException</code> <strong>error on the following line</strong> in my service under test (registrationService).</p>
  
  <pre><code>redisTemplate.opsForHash().putAll(Key.toString(), (applicationData));
  </code></pre>