首页 > 解决方案 > 当多个 Junit 测试一起运行时,@EnableCaching 会被忽略

问题描述

我有一组带有 @EnableCaching 注释的 Spring Boot 应用程序的 Junit 测试用例。当这些 Junit 测试单独运行时,它工作正常。但是当与其他 Junit 测试类一起运行时,@EnableCaching 注释似乎被忽略了。我使用 @DirtiesContext 注释在每个测试方法之后清理上下文。但这似乎对上述问题没有任何影响。

请让我知道 @EnableCaching 是否可以在 Junit 测试中使用。

请在下面找到 Junit 测试类的示例代码。

@EnableCaching
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = { "a,b,c" })
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class SampleTest {

@BeforeEach
void setUpTest() {
        //setup steps

    }
    
@Test
void testCacheable(){   
    String result = controller.testCache();

}


}

@RestController
public class TestController {

@RequestMapping("/testCache")
@Cacheable(cacheNames="cache")
public String testCache() throws InterruptedException {
        logger.info("Returning NOT from cache");
        return "cache";
    }
    
}

标签: spring-bootcachingjunit5

解决方案


推荐阅读