admin管理员组

文章数量:1124155

I am testing a Spring Boot method that uses caching with the @Cacheable annotation. The test passes successfully when run locally but fails on Concourse CI/CD with the following error:

org.opentest4j.AssertionFailedError: expected: <3> but was: <0>
    at com.hmhco.plugins.core.product.ProductBundleAltIdTypesTest.testGetProductAltKeyTypesCached(ProductBundleAltIdTypesTest.java:52)

It seems that caching is not working when the test is executed in the CI/CD environment. Instead of the expected behavior (returning cached values after the first database call), the test records 0 cache hits. Locally, the method productBundleService.getAllDistinctAltKeyTypesFromProductBundleAltId() correctly returns cached values, and the database is queried only once.

Could this issue be caused by differences in caching configuration between the local and CI/CD environments? Or is there a problem with how @Cacheable interacts with the testing framework in the CI/CD pipeline?

I am trying to test a caching feature in my ProductBundleService using the testGetProductAltKeyTypesCached method. The test passes locally but fails in the CI/CD environment (Concourse). ** This is my code: **

package com.hmhco.plugins.core.product;

import com.hmhco.BaseIntegrationTest;
import com.hmhco.plugins.core.product.model.ProductBundleAltId;
import com.hmhco.plugins.core.product.repo.ProductBundleAltIdRepository;
import com.hmhco.plugins.core.product.service.ProductBundleService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;

import java.util.List;

import static com.hmhco.plugins.utils.GeneralConstants.DBID;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@SpringBootTest(properties = {
        "spring.cache.type=caffeine",
        "spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider",
        "spring.cache.cache-names=product-bundle-alt-key-types\n",
        "spring.cache.caffeine.spec=maximumSize=1000,expireAfterAccess=10s",
        "spring.profiles.active=test"})
class ProductBundleAltIdTypesTest extends BaseIntegrationTest {
    @Autowired
    ProductBundleService productBundleService;
    @SpyBean
    ProductBundleAltIdRepository productBundleAltIdRepository;

    @BeforeEach
    public void setUp() {
        productBundleAltIdRepository.save(ProductBundleAltId.builder().altKeyType(DBID).altKey("12").build());
    }

    @AfterEach
    public void tearDown() {
        productBundleAltIdRepository.deleteAll();
    }

    @Test
    void testGetProductAltKeyTypesCached() {
        int counter = 0;
        for (int i = 0; i < 3; i++) {
            if (productBundleService.getAllDistinctAltKeyTypesFromProductBundleAltId().equals(List.of(DBID)))
                counter++;
        }
        Assertions.assertEquals(3, counter);
        verify(productBundleAltIdRepository, times(1)).getAllDistinctAltKeyIds();
    }
}

 @Cacheable(value = "product-bundle-alt-key-types")
    @Transactional(readOnly = true)
    public List<String> getAllDistinctAltKeyTypesFromProductBundleAltId() {
        return productBundleAltIdRepository.getAllDistinctAltKeyIds();
    }
  @Query("SELECT DISTINCT altKeyType FROM ProductBundleAltId")
    List<String> getAllDistinctAltKeyIds();

Additional Information There Is a Similar Test Class That Works I have another test class with a nearly identical structure, testing the same logic but with a different entity. This test passes both locally and on Concourse without any issues. The primary difference is the entity being used, but the caching configuration, repository methods, and test assertions are essentially the same.

Issue Started After Spring Boot Upgrade This issue started occurring after I upgraded Spring Boot to version 3.3.6 to resolve vulnerabilities reported by Snyk. As part of the upgrade, I had to make the following adjustments to the project: Updated various dependencies to be compatible with Spring Boot 3.3.6.

Here’s what I tried to resolve the issue:

Modified Cache Configuration I experimented with changing the cache configuration in the @SpringBootTest annotation, specifically the spring.cache.caffeine.spec property. I reduced the expiration time to expireAfterAccess=10s and increased it back to 3600s to see if it affected the test behavior. However, this did not resolve the issue.

Used @DirtiesContext Annotation I added the @DirtiesContext annotation to the test class to ensure the application context is reloaded before and after the test. Unfortunately, this also had no effect on resolving the test failure.

Despite these changes, the test continues to pass locally but fails on Concourse.

本文标签: