私はSpring Boot Integration Testの中で@Cacheableをテストするのに苦労しています。これは、統合テストを行う方法と、以前のバージョンを使用しているすべての例を学習した2日目です。私はassetEquals("some value", is())
の例を見ましたが、依存関係が "所属"であることを知るimport文は何もありません。テストでは、これは私の統合テスト....@Cacheableをテストするには?
@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、コンポーネント、エンティティ、リポジトリおよびテストのためのサービス層である第2
で失敗します。このようにしている理由は、この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();
}
'は(...)はおそらく[Hamcrest CoreMatcherの' is'](https://github.com/hamcrest)に関連しているでしょう。これはOliver Gierkeの例です。 /JavaHamcrest/blob/master/hamcrest-core/src/main/java/org/hamcrest/CoreMatchers.java#L104)。 Hamcrestは、JUnitsの 'assertThat(実際の、matcher)'と組み合わせて使用されることが多いです。なぜなら、それはより堪能なアサーションの読書スタイルを提供するからです。私は[Springキャッシュに関する質問](http://stackoverflow.com/questions/29562642/caching-of-nested-cacheable-operation-via-springcache)のある時間前に私が問題を描写するために単体テストを使ったところ。たぶんそれは何らかの形であなたに役立つでしょう –
okありがとうローマン... – Grim