но он выдает NPE.
Вот фрагмент
CacheContextConfig.java:
Код: Выделить всё
public class CacheContextConfig implements ApplicationContextAware{
@Autowired
CacheManger cacheManger;
public Cache loadCache(String cacheName, Object key){
return cacheManger.getCache(cacheName, key);
}
public void addCache(String cacheName, CacheData cacheData){
//adding data into the cache , which is loaded on application start
}
}
Код: Выделить всё
public class UserDispute{
@Autowired
CacheContextConfig cacheContextConfig;
public String manageUserDisputeData(){
//done here some business logic stuff
VaueWrapper valueWrappter = cacheContextConfig.loadCache("userStore", "userKey"); //Here cacheContextConfig is throwing NullPointerExcepion
// business logic
return result;
}
}
UserDisputeTest
Код: Выделить всё
@EnableCaching
@RunWith(MockitoJunitRunner.class)
public class UserDisputeTest{
@InjectMocks
UserDispute userDispte;
@Mock
Cache cacheMock;
@Mock
CacheManager cacheManagerMock;
@Mock
CacheContextConfig cacheContextConfigMock;
@BeforeEach
public void setConfig(){
openMocks(this);
}
public void testManageUserDisputeData(){
doReturn(cache).when(cacheManger.getCache(anyString()));
doReturn(valueWrapper).when(cacheContextConfigMock.loadCache(anyString()));
String actualResult = userDispte.manageUserDisputeData();
//Do assertion stuff
}
}
Спасибо за помощь Продолжение
Когда я попытался вызвать метод loadCache, используя имитируемый класс cacheContextConfigMock , cacheContextConfigMock выдал исключение NullPointerException.
Подробнее здесь: https://stackoverflow.com/questions/786 ... spring-boo
Мобильная версия