Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 模拟测试缓存_Java_Spring Boot_Mockito_Caffeine Cache - Fatal编程技术网

Java 模拟测试缓存

Java 模拟测试缓存,java,spring-boot,mockito,caffeine-cache,Java,Spring Boot,Mockito,Caffeine Cache,我试图用mockito测试我的缓存层 我用的是如上所述的咖啡因 基本上,我有这个 @Service class Catalog { @Autowired Db db; @Cachable public List<Item> getItems() { // fetch from db db.someDbMethod(); } } @Configuration @EnableCaching class CatalogConfig { @Bea

我试图用mockito测试我的缓存层

我用的是如上所述的咖啡因

基本上,我有这个

@Service
class Catalog {

  @Autowired
  Db db;

  @Cachable
  public List<Item> getItems() {
    // fetch from db
    db.someDbMethod();
  }
}

@Configuration
@EnableCaching
class CatalogConfig {
  @Bean
  public CacheManager cacheManager() {
    return new CaffeineCacheManager();
  }
  @Bean
  public Db db() {
     return new Db();
  }
}
// properties as in documentation etc

我试过几种组合,包括
@Profile/@ActiveProfile
Config/ContextConfiguration
等等。

我遇到过这种情况。我通过部分bean的导入和
SpringJUnit4ClassRunner
来解决它: 我将试着写出主要思想:

@RunWith(SpringJUnit4ClassRunner.class)
@Import({CaffeineCacheManager.class, Catalog.class})
public class CatalogTest {

@MockBean
private Db db;

@Autowired
private CaffeineCacheManager cache;

@Autowired
private Catalog catalog;

@Test
void cacheTest(){
   when(db.someDbMethod()).thenReturn(....);

   catalog.getItems();
   catalog.getItems();

   verify(db, times(1)).someDbMethod();

   assertTrue(cache.get(SOME_KEY).isPresent());//if you want to check that cache contains your entity
}

}

您将拥有真正的缓存bean,并有机会检查模拟数据库的调用次数,还可以在测试中获得缓存密钥。

刚刚尝试过,除了验证错误之外,我还得到了一个
java.lang.Exception:没有可运行的方法
我被告知我不应该真正测试spring缓存本身,我同意这一点。谢谢你的回答,值得一试。因此,在我的例子中,我真的想知道我的缓存和缓存逐出是否正常工作:)我很确定这是必要的,但你可以考虑其他,当然)
@RunWith(SpringJUnit4ClassRunner.class)
@Import({CaffeineCacheManager.class, Catalog.class})
public class CatalogTest {

@MockBean
private Db db;

@Autowired
private CaffeineCacheManager cache;

@Autowired
private Catalog catalog;

@Test
void cacheTest(){
   when(db.someDbMethod()).thenReturn(....);

   catalog.getItems();
   catalog.getItems();

   verify(db, times(1)).someDbMethod();

   assertTrue(cache.get(SOME_KEY).isPresent());//if you want to check that cache contains your entity
}

}