0

I get "java.lang.NoSuchMethodError: org.mockito.MockitoAnnotations.openMocks(Ljava/lang/Object;)Ljava/lang/AutoCloseable;" error when running my Unit Test. I tried some workarounds e.g. How to fix this error: java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)', but none of them fixed my problem. Here is my test method, maybe I made a mistake related to test method:

Here is my Unit Test:

@Import({ TextLabelServiceImpl.class })
@ExtendWith(SpringExtension.class)
@ImportAutoConfiguration(classes = { 
    CacheAutoConfiguration.class, RedisAutoConfiguration.class })
@EnableCaching
public class TextLabelServiceImplCaching2Test {

    private UUID uuid = null;

    @InjectMocks
    private TextLabelServiceImpl itemService;

    @Autowired
    private CacheManager cacheManager;

    @Mock
    private TextLabelTranslatableRepository translatableRepository;

    @Mock
    private TextLabelRepository textLabelRepository;

    @Test
    void givenRedisCaching_whenFindItemById_thenItemReturnedFromCache() {

        //code omitted 
        
        TextLabelDTO itemCacheMiss = itemService.findByUuid(uuid);
        verify(translatableRepository, times(1)).findAllByEntityUuid(uuid);

        TextLabelDTO itemCacheHit = itemService.findByUuid(uuid);
        verify(translatableRepository, times(1)).findAllByEntityUuid(uuid);
    }
}

So, how can I fix this problem?

Jack
  • 1
  • 21
  • 118
  • 236
  • Has anybody else ever used Caching in Unit Test? – Jack Sep 21 '21 at 19:48
  • Your test is weird (at least the combination of annotations you are trying to use). The error looks like you are messing around with both Spring test support (expecting a certain version of Mockito) and using either an older or too new version of Mockito. However you haven't included your dependencies so that will be impossible to answer. But the general reason this error occurs is due to trying to outsmart dependency management from the Spring Boot starters. – M. Deinum Sep 22 '21 at 05:30
  • @M.Deinum Thanks a lot for your useful explanations. Could you please give some useful example references (web pages and example code as answer here) for Caching Unit Test in Java? – Jack Sep 22 '21 at 06:16
  • I even doubt your test would work, as you are mocking everything, nothing would be cached. Also do you really want to test this, you are probably better off writing either an integration test using an `@Spy` and verify on that (instead of a `mock`). or rely on the fact that this is tested in Spring (Boot) already. But your current contraption isn't going to work either way. – M. Deinum Sep 22 '21 at 06:43
  • Yes, `@Spy` would be more meaningful. But as I am new for testing and not found a proper example, could you post as answer by updating necessary parts in my code? – Jack Sep 22 '21 at 07:16
  • No as I have no clue on what you want to achieve with your test. I have no clue on what should not be called when something comes from the cache, etc. so no I cannot give you a proper sample. But in short it would come down to , ditch all the annotations and just add `@SpringBootTest`, autowire the service you want to call and use `@SpyBean` on the dependency you want to verify being called only once. – M. Deinum Sep 22 '21 at 07:17

0 Answers0