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?