Open JaroslawZaczyk opened 4 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));
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 generalSession
interface fromorg.springframework.session
package, we can see internal non-public implementation calledRedisSession
. Generally in the production code it's not a big deal - we can assign the value returned to the var of typeSession
. The problem is when we want to mock this method.