junit-team / junit4

A programmer-oriented testing framework for Java.
https://junit.org/junit4
Eclipse Public License 1.0
8.51k stars 3.24k forks source link

How to test Redis Time out exception for @Cacheable method with Junit+ Spring integration test #1747

Closed BhargavChoksi closed 1 year ago

BhargavChoksi commented 1 year ago

I have a repository class in which have one repository method which call the database. I have made this method cacheable by using Spring annotation @Cacheable.

I do use Redis cache. Now, I have also implemented Spring provided CacheErrorHandler which handles the Redis runtime exceptions. However, I am not able to Unit test this implemented GlobalCacheErrorHandler class because I am not able to mock this @Cacheable method in such a way that it throws the RedisTimeOut exception.

Please find below the required code snippet which helps to understand the issue.

@Repository
Class EmployeeRepository {

    @Cacheable(cacheNames = EMPLOYEE_CACHE, cacheManager = REDIS_CACHE_MANAGER, key = “’EId_' + #p0", unless = "#result.size()==0")
    List<Employee> findByEmplyeeId(Long employeeid);

}
public class GlobalCacheErrorHandler implements CacheErrorHandler {

    public GlobalCacheErrorHandler() {
        log.info("GlobalCacheErrorHandler initialized");
    }
    @Override
    public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {

    log.error(“Could not get the value from cache”);
    }

    @Override
    public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
        log.error(“Could not put the value for the key”);
    }

    @Override
    public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
    log.error(“Could not evict value for the key");
    }

    @Override
    public void handleCacheClearError(RuntimeException exception, Cache cache) {
    log.error(“Could not clear the cache");
   }

}
@Configuration
@EnableCaching
@CustomLog
public class ApplicationCacheConfiguration extends CachingConfigurerSupport {

@Bean
@Override
public CacheErrorHandler errorHandler() {
    return new GlobalCacheErrorHandler();
}

} 

———————-

The same issue I have raised in StackOverflow as well - https://stackoverflow.com/questions/73418677/how-to-test-redis-time-out-exception-for-cacheable-method-with-junit-spring-in

sbrannen commented 1 year ago

Closing in favor of the Stack Overflow discussion.

Please do not cross-post questions like this JUnit 4, JUnit 5, Gitter, etc.

Stack Overflow is the best suited forum for discussions such as this.

BhargavChoksi commented 1 year ago

Hi @sbrannen ,

no response received in stackoverflow. It’s more than 3 weeks now. Please try to understand that we did not find any resolution for this issue. That is the reason I have raised here.

just like closing the issue, I don’t think the good way to communicate.

sbrannen commented 1 year ago

Hi @BhargavChoksi,

no response received in stackoverflow. It’s more than 3 weeks now. Please try to understand that we did not find any resolution for this issue. That is the reason I have raised here.

I understand that it can be frustrating when you're faced with a challenge and nobody helps you on Stack Overflow.

However, the team prefers to reserve this issue tracker for topics directly related to functionality provide by JUnit.

The following is our official policy regarding such issues.

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. We prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.


just like closing the issue, I don’t think the good way to communicate.

I apologize: I should have included our official policy previously.

Regards,

Sam