Я использую следующий инструмент для запуска встроенного Redis для модульного тестирования.
В начале моего registrationService создает новый экземпляр Redisserver.
@Import({RedisConfiguration.class})
@Service
public class RegistrationService
RedisTemplate redisTemplate = new RedisTemplate(); //<- new instance
public String SubmitApplicationOverview(String OverviewRequest) throws IOException {
. . .
HashMap<String,Object> applicationData = mapper.readValue(OverviewRequest,new TypeReference<Map<String,Object>>(){});
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
UUID Key = UUID.randomUUID();
redisTemplate.opsForHash().putAll(Key.toString(), (applicationData)); //<-- ERRORS HERE
System.out.println("Application saved:" + OverviewRequest);
return Key.toString();
}
}
И я запускаю фиктивный сервер redis в моем Test ниже.
...
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);
}
Настройки Redis в application.properties
#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
Однако я получаю ошибку java.lang.NullPointerException
в следующей строке в моей тестируемой службе (registrationService).
redisTemplate.opsForHash().putAll(Key.toString(), (applicationData));