如何测试@Cacheable?
How to test @Cacheable?
我正在努力在 Spring 启动集成测试中测试 @Cacheable。这是我学习如何进行集成测试的第二天,我发现的所有示例都使用旧版本。我还看到了 assetEquals("some value", is())
的示例,但没有任何导入语句可以知道 "is" 属于哪个依赖项。测试在第二个失败
这是我的集成测试....
@RunWith(SpringRunner.class)
@DataJpaTest // used for other methods
@SpringBootTest(classes = TestApplication.class)
@SqlGroup({
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
scripts = "classpath:data/Setting.sql") })
public class SettingRepositoryIT {
@Mock
private SettingRepository settingRepository;
@Autowired
private Cache applicationCache;
@Test
public void testCachedMethodInvocation() {
List<Setting> firstList = new ArrayList<>();
Setting settingOne = new Setting();
settingOne.setKey("first");
settingOne.setValue("method invocation");
firstList.add(settingOne);
List<Setting> secondList = new ArrayList<>();
Setting settingTwo = new Setting();
settingTwo.setKey("second");
settingTwo.setValue("method invocation");
secondList.add(settingTwo);
// Set up the mock to return *different* objects for the first and second call
Mockito.when(settingRepository.findAllFeaturedFragrances()).thenReturn(firstList, secondList);
// First invocation returns object returned by the method
List<Setting> result = settingRepository.findAllFeaturedFragrances();
assertEquals("first", result.get(0).getKey());
// Second invocation should return cached value, *not* second (as set up above)
List<Setting> resultTwo = settingRepository.findAllFeaturedFragrances();
assertEquals("first", resultTwo.get(0).getKey()); // test fails here as the actual is "second."
// Verify repository method was invoked once
Mockito.verify(settingRepository, Mockito.times(1)).findAllFeaturedFragrances();
assertNotNull(applicationCache.get("findAllFeaturedFragrances"));
// Third invocation with different key is triggers the second invocation of the repo method
List<Setting> resultThree = settingRepository.findAllFeaturedFragrances();
assertEquals(resultThree.get(0).getKey(), "second");
}
}
ApplicationContext、组件、实体、存储库和用于测试的服务层。我之所以这样做是因为这个maven模块在其他模块中作为依赖使用
@ComponentScan({ "com.persistence_common.config", "com.persistence_common.services" })
@EntityScan(basePackages = { "com.persistence_common.entities" })
@EnableJpaRepositories(basePackages = { "com.persistence_common.repositories" })
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
缓存配置....
@Configuration
@EnableCaching
public class CacheConfig {
public static final String APPLICATION_CACHE = "applicationCache";
@Bean
public FilterRegistrationBean registerOpenSessionInViewFilterBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
registrationBean.setFilter(filter);
registrationBean.setOrder(5);
return registrationBean;
}
@Bean
public Cache applicationCache() {
return new GuavaCache(APPLICATION_CACHE, CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.DAYS)
.build());
}
}
正在测试的存储库....
public interface SettingRepository extends JpaRepository<Setting, Integer> {
@Query(nativeQuery = true, value = "SELECT * FROM Setting WHERE name = 'featured_fragrance'")
@Cacheable(value = CacheConfig.APPLICATION_CACHE, key = "#root.methodName")
List<Setting> findAllFeaturedFragrances();
}
SettingRepositoryIT 的第一个问题是,字段 settingRepository 上的 @Mock 注释.这对于任何正常测试、集成测试或任何其他测试都是自相矛盾的。
您应该让 Spring 引入 class-under-test 的依赖项,即 SettingRepository 在你的情况下。
请看这个例子 @Autowired 是如何用于 class-under-test,这是 OrderService 在这个例子中:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from the
// static nested Config class
@ContextConfiguration
public class OrderServiceTest {
@Configuration
static class Config {
// this bean will be injected into the OrderServiceTest class
@Bean
public OrderService orderService() {
OrderService orderService = new OrderServiceImpl();
// set properties, etc.
return orderService;
}
}
@Autowired
private OrderService orderService;
@Test
public void testOrderService() {
// test the orderService
}
}
获取包含完整示例的文档:§ 15. Integration Testing
第二个问题是你不必测试@Cachable。您应该只测试您的实施。以下是 Oliver Gierke 提供的关于如何测试它的一个很好的例子:How to test Spring's declarative caching support on Spring Data repositories?
在我的例子中,我想验证 @Cacheable 注释中 unless 表达式中的表达式,所以我认为它非常有意义,我没有测试 Spring 的代码。
我设法在不使用 Spring 引导的情况下对其进行了测试,所以很简单 Spring 测试:
@RunWith(SpringRunner.class)
@ContextConfiguration
public class MyTest {
private static MyCacheableInterface myCacheableInterfaceMock = mock(MyCacheableInterface.class);
@Configuration
@EnableCaching
static class Config {
@Bean
public MyCacheableInterface myCacheableInterface() {
return myCacheableInterfaceMock;
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("myObject");
}
}
@Autowired
private MyCacheableInterface myCacheableInterface;
@Test
public void test() {
when(myCacheableInterfaceMock.businessMethod(anyString())).then(i -> {
List<MyObject> list = new ArrayList<>();
list.add(new MyObject(new Result("Y")));
return list;
});
myCacheableInterface.businessMethod("test");
verify(myCacheableInterfaceMock).businessMethod(anyString());
myCacheableInterface.businessMethod("test");
verifyNoMoreInteractions(myCacheableInterfaceMock);
}
}
在 MyCacheableInterface 中我有以下注释:
public interface MyCacheableInterface {
@Cacheable(value = "myObject", unless = "#result.?[Result.getSuccess() != 'Y'].size() == #result.size()")
List<MyObject> businessMethod(String authorization);
}
我正在努力在 Spring 启动集成测试中测试 @Cacheable。这是我学习如何进行集成测试的第二天,我发现的所有示例都使用旧版本。我还看到了 assetEquals("some value", is())
的示例,但没有任何导入语句可以知道 "is" 属于哪个依赖项。测试在第二个失败
这是我的集成测试....
@RunWith(SpringRunner.class)
@DataJpaTest // used for other methods
@SpringBootTest(classes = TestApplication.class)
@SqlGroup({
@Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD,
scripts = "classpath:data/Setting.sql") })
public class SettingRepositoryIT {
@Mock
private SettingRepository settingRepository;
@Autowired
private Cache applicationCache;
@Test
public void testCachedMethodInvocation() {
List<Setting> firstList = new ArrayList<>();
Setting settingOne = new Setting();
settingOne.setKey("first");
settingOne.setValue("method invocation");
firstList.add(settingOne);
List<Setting> secondList = new ArrayList<>();
Setting settingTwo = new Setting();
settingTwo.setKey("second");
settingTwo.setValue("method invocation");
secondList.add(settingTwo);
// Set up the mock to return *different* objects for the first and second call
Mockito.when(settingRepository.findAllFeaturedFragrances()).thenReturn(firstList, secondList);
// First invocation returns object returned by the method
List<Setting> result = settingRepository.findAllFeaturedFragrances();
assertEquals("first", result.get(0).getKey());
// Second invocation should return cached value, *not* second (as set up above)
List<Setting> resultTwo = settingRepository.findAllFeaturedFragrances();
assertEquals("first", resultTwo.get(0).getKey()); // test fails here as the actual is "second."
// Verify repository method was invoked once
Mockito.verify(settingRepository, Mockito.times(1)).findAllFeaturedFragrances();
assertNotNull(applicationCache.get("findAllFeaturedFragrances"));
// Third invocation with different key is triggers the second invocation of the repo method
List<Setting> resultThree = settingRepository.findAllFeaturedFragrances();
assertEquals(resultThree.get(0).getKey(), "second");
}
}
ApplicationContext、组件、实体、存储库和用于测试的服务层。我之所以这样做是因为这个maven模块在其他模块中作为依赖使用
@ComponentScan({ "com.persistence_common.config", "com.persistence_common.services" })
@EntityScan(basePackages = { "com.persistence_common.entities" })
@EnableJpaRepositories(basePackages = { "com.persistence_common.repositories" })
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
缓存配置....
@Configuration
@EnableCaching
public class CacheConfig {
public static final String APPLICATION_CACHE = "applicationCache";
@Bean
public FilterRegistrationBean registerOpenSessionInViewFilterBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
OpenEntityManagerInViewFilter filter = new OpenEntityManagerInViewFilter();
registrationBean.setFilter(filter);
registrationBean.setOrder(5);
return registrationBean;
}
@Bean
public Cache applicationCache() {
return new GuavaCache(APPLICATION_CACHE, CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.DAYS)
.build());
}
}
正在测试的存储库....
public interface SettingRepository extends JpaRepository<Setting, Integer> {
@Query(nativeQuery = true, value = "SELECT * FROM Setting WHERE name = 'featured_fragrance'")
@Cacheable(value = CacheConfig.APPLICATION_CACHE, key = "#root.methodName")
List<Setting> findAllFeaturedFragrances();
}
SettingRepositoryIT 的第一个问题是,字段 settingRepository 上的 @Mock 注释.这对于任何正常测试、集成测试或任何其他测试都是自相矛盾的。
您应该让 Spring 引入 class-under-test 的依赖项,即 SettingRepository 在你的情况下。
请看这个例子 @Autowired 是如何用于 class-under-test,这是 OrderService 在这个例子中:
@RunWith(SpringRunner.class)
// ApplicationContext will be loaded from the
// static nested Config class
@ContextConfiguration
public class OrderServiceTest {
@Configuration
static class Config {
// this bean will be injected into the OrderServiceTest class
@Bean
public OrderService orderService() {
OrderService orderService = new OrderServiceImpl();
// set properties, etc.
return orderService;
}
}
@Autowired
private OrderService orderService;
@Test
public void testOrderService() {
// test the orderService
}
}
获取包含完整示例的文档:§ 15. Integration Testing
第二个问题是你不必测试@Cachable。您应该只测试您的实施。以下是 Oliver Gierke 提供的关于如何测试它的一个很好的例子:How to test Spring's declarative caching support on Spring Data repositories?
在我的例子中,我想验证 @Cacheable 注释中 unless 表达式中的表达式,所以我认为它非常有意义,我没有测试 Spring 的代码。
我设法在不使用 Spring 引导的情况下对其进行了测试,所以很简单 Spring 测试:
@RunWith(SpringRunner.class)
@ContextConfiguration
public class MyTest {
private static MyCacheableInterface myCacheableInterfaceMock = mock(MyCacheableInterface.class);
@Configuration
@EnableCaching
static class Config {
@Bean
public MyCacheableInterface myCacheableInterface() {
return myCacheableInterfaceMock;
}
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("myObject");
}
}
@Autowired
private MyCacheableInterface myCacheableInterface;
@Test
public void test() {
when(myCacheableInterfaceMock.businessMethod(anyString())).then(i -> {
List<MyObject> list = new ArrayList<>();
list.add(new MyObject(new Result("Y")));
return list;
});
myCacheableInterface.businessMethod("test");
verify(myCacheableInterfaceMock).businessMethod(anyString());
myCacheableInterface.businessMethod("test");
verifyNoMoreInteractions(myCacheableInterfaceMock);
}
}
在 MyCacheableInterface 中我有以下注释:
public interface MyCacheableInterface {
@Cacheable(value = "myObject", unless = "#result.?[Result.getSuccess() != 'Y'].size() == #result.size()")
List<MyObject> businessMethod(String authorization);
}