title: Cache is closed causing an exception while running test suite
body: <p>I'm experiencing a problem similar to the described in <a href="https://stackoverflow.com/q/16281802/1429387">this question</a>.</p>
  
  <p>I have a test suite that runs fine in development environment. One of the tests fails when executed in Bitbucket Pipelines with the following exception:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
      at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
     ....
  </code></pre>
  
  <p>I want to try the <a href="https://stackoverflow.com/a/16370326/1429387">accepted solution</a> but I don't know how to apply it to my project. The <a href="https://stackoverflow.com/a/45030054/1429387">second solution</a> depends on ehcache.xml file. I don't have this file, everything is configured in JavaConfig. How can I adopt the proposed solutions for <strong>EhCache + JCache</strong> (JSR-107) in JavaConfig?</p>
  
  <p>My cache configuration:</p>
  
  <pre><code>@Configuration
  @EnableCaching
  public class CacheConfig {
  
      private final javax.cache.configuration.Configuration&lt;Object, Object&gt; jcacheConfiguration =
              Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
                      .newCacheConfigurationBuilder(Object.class, Object.class,
                              ResourcePoolsBuilder.newResourcePoolsBuilder()
                                      .heap(100, EntryUnit.ENTRIES))
                      .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
                      .build());
  
      @Bean
      public JCacheManagerCustomizer cacheManagerCustomizer() {
          return cm -&gt; {
              createIfNotExists(cm, "model.Role");
              createIfNotExists(cm, "model.User.roles");
              // ...
          };
      }
  
      private void createIfNotExists(CacheManager cacheManager, String cacheName) {
          if (cacheManager.getCache(cacheName) == null) {
              cacheManager.createCache(cacheName, jcacheConfiguration);
          }
      }
  }
  </code></pre>
  
  <p>Gradle dependencies:</p>
  
  <pre><code>implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
  implementation group: 'javax.cache', name: 'cache-api'
  implementation group: 'org.ehcache', name: 'ehcache'
  implementation group: 'org.hibernate', name: 'hibernate-jcache'
  </code></pre>
  
  <p>The failing test:</p>
  
  <pre><code>@SpringBootTest
  @RunWith(SpringJUnit4ClassRunner.class)
  public class SecondLevelCacheTest {
      @Autowired
      private RoleRepository roleRepository;
      private CacheManager manager;
  
      @Before
      public void initCacheManager() {
          CachingProvider provider = Caching.getCachingProvider();
          manager = provider.getCacheManager();
  
          final String cacheRegion = "model.Role";
          manager.getCache(cacheRegion).clear();
      }
  
      @Test
      public final void givenEntityIsLoaded_thenItIsCached() {
          final String cacheRegion = "model.Role";
  
          boolean hasNext = manager.getCache(cacheRegion).iterator().hasNext();
          final Role role = roleRepository.findByName("USER");
          boolean hasNext2 = manager.getCache(cacheRegion).iterator().hasNext();
          final Role role2 = roleRepository.findByName("USER");
  
          Assert.assertFalse(hasNext);
          Assert.assertTrue(hasNext2);
      }
  }
  </code></pre>
  
  <p>The most upvoted solution is "to set shared property to false in the testing context." How can I do this with regard to my configuration?</p>