spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.87k stars 1.12k forks source link

Wrong return type in findById method #1729

Open JaroslawZaczyk opened 4 years ago

JaroslawZaczyk commented 4 years ago

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.

pawelrmi commented 3 years ago

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));