Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2222

Problems with test Redis cache with @MockBean

$
0
0

I'm trying to test an implementation of cache with redis and when I debug, I could check the repository is null and I don't know if it's ok with it.

enter image description here

But the test fails:INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.example.pocredis.cache.AnyObjectCachingIntegrationTest]: AnyObjectCachingIntegrationTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

Wanted but not invoked:com.example.pocredis.repository.AnyObjectRepository#0 bean.findById(1L);-> at com.example.pocredis.cache.AnyObjectCachingIntegrationTest.verify_findById_cache_interaction_with_repository(AnyObjectCachingIntegrationTest.java:52)Actually, there were zero interactions with this mock.

Wanted but not invoked:com.example.pocredis.repository.AnyObjectRepository#0 bean.findById(1L);-> at com.example.pocredis.cache.AnyObjectCachingIntegrationTest.verify_findById_cache_interaction_with_repository(AnyObjectCachingIntegrationTest.java:52)Actually, there were zero interactions with this mock.

at com.example.pocredis.cache.AnyObjectCachingIntegrationTest.verify_findById_cache_interaction_with_repository(AnyObjectCachingIntegrationTest.java:52)at java.base/java.lang.reflect.Method.invoke(Method.java:568)at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Process finished with exit code -1

Config class:

@Configuration@EnableCachingpublic class RedisCacheConfig {    @Bean    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()            .entryTtl(Duration.ofMinutes(10))            .disableCachingNullValues();        return RedisCacheManager.builder(connectionFactory)                .cacheDefaults(cacheConfiguration)                .build();    }    @Bean    public HashKey hashKey() {        return new HashKey();    }}

Service class:

@Service@RequiredArgsConstructorpublic class AnyObjectService {    private final AnyObjectRepository repository;    @Cacheable(value = "anyObjects", key = "#id")    public AnyObject findById(Long id) {        return repository.findById(id).orElseThrow(() -> new AnyObjectNotFoundException(id));    }}

Test class:

@Import({ RedisCacheConfig.class, AnyObjectService.class})@ExtendWith(SpringExtension.class)@EnableCaching@ImportAutoConfiguration(classes = {        CacheAutoConfiguration.class,        RedisAutoConfiguration.class})public class AnyObjectCachingIntegrationTest {    @MockBean    private AnyObjectRepository repository;    @Autowired    private AnyObjectService service;    @Autowired    private CacheManager cacheManager;    @Test    void verify_findById_cache_interaction_with_repository() {        AnyObject obj = createValidAnyObject();        when(repository.findById(obj.getId())).thenReturn(Optional.of(obj));        service.findById(obj.getId());        service.findById(obj.getId());        verify(repository, times(1)).findById(obj.getId());    }}

Viewing all articles
Browse latest Browse all 2222

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>