ehcache3配置类单元测试

5fjcxozz  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(220)

我正在使用ehcache3和spring,我想对这个配置类进行单元测试。

@Configuration
@EnableCaching
public class EhCacheConfiguration {

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cacheManager -> {
            Cache<SimpleKey, Try.Success<Person>> cache = cacheManager .getCache("cache");
            if (cache == null) {
                cacheManager .createCache("cache", jcacheConfiguration());
            }
        };
    }

    private javax.cache.configuration.Configuration<SimpleKey, Try.Success> jcacheConfiguration() {
        return Eh107Configuration.fromEhcacheCacheConfiguration(
                CacheConfigurationBuilder
                        .newCacheConfigurationBuilder(
                                SimpleKey.class,
                                Try.Success.class,
                                ResourcePoolsBuilder.heap(2000).offheap(25, MemoryUnit.MB)
                        )
                        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofHours(timeToLive)))
                        .build()
        );
    }
}

我尝试了这个测试类,但是我不能在bean cachemanagercustomizer中模拟cachemanager。我试过用mockbean,mock,mocks,但是我迷路了

@ExtendWith(MockitoExtension.class)
class EhCacheConfigurationTest {

    private EhCacheConfiguration cut;

    @MockBean
    private CacheManager cacheManager;

    @BeforeEach
    void setUp() {
        cut = new EhCacheConfiguration();
        MockitoAnnotations.initMocks(this);
    }

    @Test
    void checkIfCacheIsCalled() {
        assertThat(cut.cacheManagerCustomizer()).isInstanceOf(JCacheManagerCustomizer.class);

        verify(cacheManager).getCache("cache");
    }
}

我收到以下错误消息:

org.mockito.exceptions.misusing.NullInsteadOfMockException: 
Argument passed to verify() should be a mock but is null!

如何在cachemanagercustomizer bean中模拟cachemanager bean?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题