我正在使用following工具运行嵌入式redis进行单元测试。
在 registrationService 的开头,正在创建一个新的redis服务器实例。
@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服务器。
...
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);
}
application.properties
中的Redis设置#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
但是,我的测试服务(registrationService)中的以下行出现java.lang.NullPointerException
错误。
redisTemplate.opsForHash().putAll(Key.toString(), (applicationData));
答案 0 :(得分:0)
根据[redis-mock][1]
文档,创建一个这样的实例:
RedisServer.newRedisServer(); // bind to a random port
将实例绑定到随机端口。看起来您的代码需要特定的端口。我相信您需要在创建服务器时通过传递如下端口号来指定端口:
RedisServer.newRedisServer(8000); // bind to a random port