spring-session icon indicating copy to clipboard operation
spring-session copied to clipboard

Wrong return type in findById method

Open JaroslawZaczyk opened this issue 5 years ago • 1 comments

https://github.com/spring-projects/spring-session/blob/5f5168814d9bf35bee3b8965ffde7bb1ae91cd93/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java#L428

In my particular case when I had to mock up this class in my tests, it's impossible to mock return type of findById(String id) method because instead of general Session interface from org.springframework.session package, we can see internal non-public implementation called RedisSession. Generally in the production code it's not a big deal - we can assign the value returned to the var of type Session. The problem is when we want to mock this method.

JaroslawZaczyk avatar Nov 16 '20 13:11 JaroslawZaczyk

I solved this issue by injecting SessionRepository interface instead of RedisIndexedSessionRepository:

private final SessionRepository<? extends Session> sessionRepository;

Then you can mock it:

when(sessionRepository.findById(GENERATED_ID)).thenReturn(mock(Session.class));

pawelrmi avatar Nov 09 '21 08:11 pawelrmi