Source:
public void test() throws InterruptedException, IOException { DefaultManagementRegistryConfiguration registryConfiguration = new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCacheManager"); registryConfiguration.addConfiguration(new EhcacheStatisticsProviderConfiguration(1,TimeUnit.MINUTES,100,1,TimeUnit.MILLISECONDS,10,TimeUnit.MINUTES)); ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(registryConfiguration); Configuration cacheConfiguration = new DefaultConfiguration(EvictionTest.class.getClassLoader(), new DefaultPersistenceConfiguration(diskPath.newFolder())); Collection<Service> services = new ArrayList<Service>(); services.add(managementRegistry); CacheManager cacheManager = null; try { cacheManager = new EhcacheManager(cacheConfiguration, services); CacheConfiguration<Long, String> cacheConfig = newCacheConfigurationBuilder(Long.class, String.class, resources).build(); cacheManager.init(); Cache<Long, String> cache = cacheManager.createCache("myCache", cacheConfig); cache.put(1L, "1"); cache.put(2L, "2"); cache.put(3L, "3"); for(Long key : getKeys) { cache.get(key); } Context context = StatsUtil.createContext(managementRegistry); double tierHitRatio = 0; for (int i = 0; i < statNames.size(); i++) { tierHitRatio = StatsUtil.getExpectedValueFromRatioHistory(statNames.get(i), context, managementRegistry, tierExpectedValues.get(i)); Assert.assertThat(tierHitRatio, is(tierExpectedValues.get(i))); } double hitRatio = StatsUtil.getExpectedValueFromRatioHistory("Cache:HitRatio", context, managementRegistry, cacheExpectedValue); Assert.assertThat(hitRatio, is(cacheExpectedValue)); } finally { if(cacheManager != null) { cacheManager.close(); } } }

Target:
public void test() throws InterruptedException, IOException { CacheManager cacheManager = null; try { DefaultManagementRegistryConfiguration registryConfiguration = new DefaultManagementRegistryConfiguration().setCacheManagerAlias("myCacheManager"); registryConfiguration.addConfiguration(new EhcacheStatisticsProviderConfiguration(1,TimeUnit.MINUTES,100,1,TimeUnit.MILLISECONDS,10,TimeUnit.MINUTES)); ManagementRegistryService managementRegistry = new DefaultManagementRegistryService(registryConfiguration); CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class, resources).build(); cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .withCache("myCache", cacheConfiguration) .using(managementRegistry) .using(new DefaultPersistenceConfiguration(diskPath.newFolder())) .build(true); Cache<Long, String> cache = cacheManager.getCache("myCache", Long.class, String.class); cache.put(1L, "1"); cache.put(2L, "2"); cache.put(3L, "3"); for(Long key : getKeys) { cache.get(key); } Context context = StatsUtil.createContext(managementRegistry); double tierHitRatio = 0; for (int i = 0; i < statNames.size(); i++) { tierHitRatio = StatsUtil.getExpectedValueFromRatioHistory(statNames.get(i), context, managementRegistry, tierExpectedValues.get(i)); Assert.assertThat(tierHitRatio, is(tierExpectedValues.get(i))); } double hitRatio = StatsUtil.getExpectedValueFromRatioHistory("Cache:HitRatio", context, managementRegistry, cacheExpectedValue); Assert.assertThat(hitRatio, is(cacheExpectedValue)); } finally { if(cacheManager != null) { cacheManager.close(); } } }