Search code examples
javaspringmaven-3spring-testcache2k

Multiple tests with @SpringBootTest fail during Maven build in Spring Cache project with Cache2K


I have a Spring Boot REST project with multiple @SpringBootTest JUnit test cases.

The project uses Spring Cache with Cache2K. There is a factory bean that creates a CacheManager with a cache.

@Bean
public CacheManager cache2kCacheManager() {
    SpringCache2kCacheManager cache2kCacheManager = new SpringCache2kCacheManager()
            .defaultSetup(b -> b.entryCapacity(3).expireAfterWrite(3, TimeUnit.SECONDS).disableMonitoring(false));
    Function<Cache2kBuilder<?, ?>, Cache2kBuilder<?, ?>> campaignCacheBuilder;
    campaignCacheBuilder = x -> x.name("campaigns-cache")
            .entryCapacity(5));
    cache2kCacheManager.addCaches(campaignCacheBuilder);
    return cache2kCacheManager;
}

All the test cases run successfully when run in the IDE. The application also runs when launched in the IDE. However when I run mvn clean install on the project, the test cases fail due to Cache object creation error.

[ERROR] testCacheReadAndWrite  Time elapsed: 0 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cache2kCacheManager' defined in class path resource [com/demos/cachedemo/cache/configuration/Cache2KConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cache2kCacheManager' threw exception; nested exception is java.lang.IllegalStateException: Cache already created: 'campaigns-cache'
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'cache2kCacheManager' threw exception; nested exception is java.lang.IllegalStateException: Cache already created: 'campaigns-cache'
Caused by: java.lang.IllegalStateException: Cache already created: 'campaigns-cache'

I tried removing the test cases with error, but others start failing with the same exception. It appears that the context is being reused/shared. I have already added @DirtiesContext to the test cases, but that does not fix the issue.

Can anyone help me with this issue?

Update 1: The project was created using start.spring.io and has the default build plugin in pom.xml.

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <excludes>
          <exclude>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          </exclude>
        </excludes>
      </configuration>
    </plugin>
  </plugins>
</build>

Solution

  • The issue was due to the Maven Surefire tests running in parallel. This answer provided a solution which solved the problem.

    The project was created using start.spring.io and had default build plugins (I have edited the question with the previous build configuration).

    I added the following Surefire configuration to limit the parallel runs.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <reuseForks>false</reuseForks>
                    <forkCount>1</forkCount>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    If there is a better solution, please post it.