- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在努力在 Spring Boot 集成测试中测试 @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?
关于java - 如何测试@Cacheable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948434/
我正在尝试使用 Spring Cacheable,但遇到了类转换异常 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(cla
我就废话不多说了,大家还是直接看代码吧~ ? 1
我有这个实用方法来加载我的网络应用程序的内容 @Cacheable(value="dataStrore" ) public MarketJson retrieveMarketJson(DataRequ
与内存数据库一起使用@Cacheable是否相关? 我在内存数据库中使用 h2,只是想知道使用 @Cacheable 可以提高应用程序的性能。我的应用程序将频繁调用数据库中的静态数据 最佳答案 一般来
将 Spring 3.2 与 EhCache 2.9 一起使用。我已经注释了一个零参数方法如下: @Cacheable(value="myList", key="#result.method.name
我正在尝试将 Redis 与 Spring 的 @Cacheable 一起使用,但需要根据 Spring Boot 样式的应用程序属性有条件地打开或关闭缓存。我的第一次尝试似乎没有用。 applica
我在 Spring 3.2 中使用 @Cacheable 来缓存服务层方法的结果。服务类内部使用以下方法代码: @Cacheable("questions") public List getSecut
如何查看@Cacheable 的实现。我想了解他们如何能够获取方法参数的名称并在运行时获取其实际值。 例如: @Cacheable(cacheNames = "name", key = "#k
我正在努力在 Spring Boot 集成测试中测试 @Cacheable。这是我学习如何进行集成测试的第二天,我发现的所有示例都使用旧版本。我还看到了 assetEquals("some value
背景 项目中,使用@Cacheable进行数据缓存。发现:当redis宕机之后,@Cacheable注解的方法并未进行缓存冲突,而是直接抛出异常。而这样的异常会导致服务不可用。 原因分析 我们
我在 Spring 4.1.4 应用程序中使用最新的 Ehcache。我所拥有的是: class Contact{ int id; int revision; } @Cachea
我已经看到很多同一个问题的化身,但我想我已经尝试了所有修复 - 我的用法非常简单。 我一直在使用 Ehcache,但也没有用。因此,为了排除 Ehcache 问题并帮助指出更基本的东西,我转向了 Si
我有一个关于 Spring Security 和 Spring Caching 的问题。假设我有一个方法,并且我已经用 @PreAuthorize("condition") 和 @Cacheable(
我一般用@Cacheable在我的 spring-boot 应用程序中使用缓存配置并为每个缓存设置特定的 TTL(生存时间)。 我最近继承了一个使用 @Cacheable 的 spring boot
我想将一项服务设为可缓存。我一直在研究grails-cache plugin它看起来很有希望,但它导致了一些我不理解的行为。 考虑以下服务: class FooService { def co
我在使用多参数和分页的 @Cacheable 时遇到问题。 @Cacheable(value = "books", key = "#p0") public List findBooks(Long lo
当调用带有 @Cacheable 注释且同步标志设置为 true 的方法时,出现以下异常: java.lang.AbstractMethodError: org.springframework.cac
我已将我的应用程序升级到 Spring 3.1,并且所有 jar 都已充分更新。但是,当我尝试对其中一个 Controller 中的方法使用 @Cacheable 时,该 Controller 的所有
如何进行以下工作? public abstract class MyAbstractOne { @Cacheable(value="myCache") public MyObj
尝试使用内联刷新来实现我自己的缓存加载器。此缓存加载使用 RefreshAheadCacheFactory 如描述 http://terracotta.org/documentation/4.1/bi
我是一名优秀的程序员,十分优秀!