Redis unit test using redis mock

Kaigo

I'm using the following tool to run an embedded redis for unit testing.

In the beginning of my registrationService is creating a new instance of redis server.

@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();
 }
}

And I'm starting a mock redis server in my Test below.

...

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 protected]\"\n" +
            "}";
    String result = RegistrationService.SubmitApplicationOverview(body);

    Assert.assertEquals("something", result);
}

Redis settings in 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

However, I'm getting a java.lang.NullPointerException error on the following line in my service under test (registrationService).

redisTemplate.opsForHash().putAll(Key.toString(), (applicationData));
Andrew Eisenberg

According to the [redis-mock][1] documentation, creating an instance like this:

RedisServer.newRedisServer();  // bind to a random port

Will bind the instance to a random port. It looks like your code expects a specific port. I believe that you need to specify a port when you create the server by passing a port number like this:

RedisServer.newRedisServer(8000);  // bind to a specific port

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using Moq to mock an asynchronous method for a unit test

From Dev

Mock HttpContext using moq for unit test

From Dev

How to unit test Service Stacks Redis Client with Moq

From Dev

Using free monads to test Redis calls

From Dev

Mock IMemoryCache in unit test

From Dev

How to mock SoapException using Moq to unit test error handling

From Dev

How to check if a function was called in a unit test using pytest-mock?

From Dev

How to mock a redis client in Python?

From Dev

Mock/Stub a RuntimeException in unit test

From Dev

Spock Mock not working for unit test

From Dev

How to mock springSecurityService in an unit test

From Dev

How to mock this unit test in Python?

From Dev

Unit test a void method with Mock?

From Dev

How to use Redis in unit testing?

From Dev

How to use Redis in unit testing?

From Dev

Spring session with redis - how to mock it in integration tests?

From Dev

mock @patch does not patch redis class

From Dev

Using Redis with SignalR

From Dev

Using ServiceStack Redis with Twemproxy

From Dev

Using Redis with magento 1.9

From Dev

Mutual Exclusion using Redis

From Dev

Pagination using Redis and PHP

From Dev

Using redis in an Express route

From Dev

Using Redis to implement login?

From Dev

Unit test command class with mock repository

From Dev

How to android unit test and mock a static method

From Dev

Correct way to mock an AngularJS Service in a unit test?

From Dev

how to mock $window to unit test AngularJS service?

From Dev

AngularJS override (mock) services for unit test