首页 > 解决方案 > java.lang.NoSuchMethodError: org.mockito.MockitoAnnotations.openMocks()... 单元测试中的错误

问题描述

运行单元测试时出现“ java.lang.NoSuchMethodError: org.mockito.MockitoAnnotations.openMocks(Ljava/lang/Object;)Ljava/lang/Au​​toCloseable; ”错误。我尝试了一些解决方法,例如How to fix this error: java.lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)',但它们都没有解决我的问题。这是我的测试方法,也许我犯了与测试方法有关的错误:

这是我的单元测试:

@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);
    }
}

那么,我该如何解决这个问题呢?

标签: javaspringunit-testingtestingredis

解决方案


推荐阅读