Java 使用for循环从CacheManager检索数据的方法的单元测试
我有一个方法,可以从下一个webapi中检索人员并将其存储在缓存中,我希望从缓存管理器中获取相同的缓存数据。我很难为这种方法编写单元测试。 非常感谢您的帮助Java 使用for循环从CacheManager检索数据的方法的单元测试,java,spring-boot,junit,ehcache,Java,Spring Boot,Junit,Ehcache,我有一个方法,可以从下一个webapi中检索人员并将其存储在缓存中,我希望从缓存管理器中获取相同的缓存数据。我很难为这种方法编写单元测试。 非常感谢您的帮助 import javax.cache.Cache; import javax.cache.CacheManager; @Autowired @Qualifier(value = "cacheManager") private CacheManager cacheManager; *public List<Person> fa
import javax.cache.Cache;
import javax.cache.CacheManager;
@Autowired
@Qualifier(value = "cacheManager")
private CacheManager cacheManager;
*public List<Person> fallbackPersons() {
List<Person> data = new ArrayList<>();
for (Cache.Entry<Object, Object> entry :cacheManager.getCache("person"){
data = (List<Person>) entry.getValue();
}
return data;
}*
import javax.cache.cache;
导入javax.cache.CacheManager;
@自动连线
@限定符(value=“cacheManager”)
专用缓存管理器缓存管理器;
*公众名单后备人员(){
列表数据=新的ArrayList();
对于(Cache.Entry:cacheManager.getCache(“person”){
data=(List)entry.getValue();
}
返回数据;
}*
1.单元测试
如果您打算通过模拟CacheManager
对公共方法fallbackPersons
进行单元测试,我强烈建议您使用构造函数注入更改注入CacheManager bean的样式:
import javax.cache.Cache;
import javax.cache.CacheManager;
@Service
public class PersonsService {
private final CacheManager cacheManager;
@Autowired
public PersonsService(@Qualifier(value = "cacheManager") CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
public List<Person> fallbackPersons() {
List<Person> data = new ArrayList<>();
for (Cache.Entry<Object, Object> entry : cacheManager.getCache("person")) {
data = (List<Person>) entry.getValue();
}
return data;
}
}
2.集成测试
如果您确实希望使用Spring提供的real cache manager实现测试您的服务,那么您应该研究如何使用Spring Framework提供的
AbstractJUnit4SpringContextTests
或AbstractTestNGSpringContextTests
类的示例。它们将允许您初始化真正的Spring上下文并注入真正的CacheManager实现,如EhCahce等。您可以模拟CacheManager,将其存根并验证结果,如下所示:
@RunWith(MockitoJUnitRunner.class)
public class PersonsServiceTest {
@Mock
private CacheManager cacheManager;
@InjectMocks
PersonsService service = new PersonsService();
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void fallbackPersonsWithNonEmptyCache() {
List<Person> persons = Collections.singletonList(new Person()); // create person object as your Person class definition
// mock cache entry
Cache.Entry <Object, Object> entry = Mockito.mock(Cache.Entry.class);
// do stubbing
Mockito.when(entry.getValue()).thenReturn(persons);
Mockito.when(cacheManager.getCache(Matchers.anyString()))
.thenReturn(entry);
// execute
List<Person> persons = service.fallbackPersons();
// verify
Assert.assertNotNull(persons);
Assert.assertFalse(persons.isEmpty());
}
}
@RunWith(MockitoJUnitRunner.class)
公共类PersonsServiceTest{
@嘲弄
专用缓存管理器缓存管理器;
@注射模拟
PersonsService=新的PersonsService();
@以前
公共作废设置(){
initMocks(this);
}
@试验
public void fallbackPersonwithnonemptycache(){
List persons=Collections.singletonList(new Person());//创建Person对象作为Person类定义
//模拟缓存项
Cache.Entry=Mockito.mock(Cache.Entry.class);
//打短棍
Mockito.when(entry.getValue()).thenReturn(persons);
Mockito.when(cacheManager.getCache(Matchers.anyString()))
.然后返回(条目);
//执行
List persons=service.fallbackPersons();
//核实
Assert.assertNotNull(人);
Assert.assertFalse(persons.isEmpty());
}
}
我的问题的正确解决方案
@ActiveProfiles("test")
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class CacheTest {
@Spy
@InjectMocks
PersonService personService;
@Mock
private CacheManager cacheManager;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void fallbackMCCsWithNonEmptyCache() {
List<Person> persons = Collections.singletonList(new Person());
Iterator<Cache.Entry <Object, Object>> cacheIterator =
Mockito.mock(Iterator.class);
Cache <Object, Object> cache = Mockito.mock(Cache.class);
Cache.Entry <Object, Object> entry = Mockito.mock(Cache.Entry.class);
Mockito.when(cacheManager.getCache(Mockito.anyString()))
.thenReturn(cache);
Mockito.when(cache.iterator()).thenReturn(cacheIterator);
List<Person> personList = personService.fallbackPersons();
Assert.assertNotNull(personList);
Assert.assertTrue(personList.isEmpty());
}
}
@ActiveProfiles(“测试”)
@RunWith(MockitoJUnitRunner.class)
@Slf4j
公共类缓存测试{
@间谍
@注射模拟
人情服务人情服务;
@嘲弄
专用缓存管理器缓存管理器;
@以前
公共作废设置(){
initMocks(this);
}
@试验
公共无效回退mccswithnonemptycache(){
List Person=Collections.singletonList(newperson());
迭代器缓存迭代器=
Mockito.mock(迭代器类);
Cache Cache=Mockito.mock(Cache.class);
Cache.Entry=Mockito.mock(Cache.Entry.class);
Mockito.when(cacheManager.getCache(Mockito.anyString()))
.然后返回(缓存);
Mockito.when(cache.iterator()).thenReturn(cacheIterator);
List personList=personService.fallbackPersons();
Assert.assertNotNull(personList);
Assert.assertTrue(personList.isEmpty());
}
}
感谢您的回复。第行下面应该返回缓存,而不是emptyList Mockito。when(cacheManager.getCache(Matchers.anyString())。然后返回(Collections.emptyList());另外,我仍然从(Cache.Entry:cacheManager.getCache(“person”)行获取NullPointerExcetpion{@suraz我已经更新了示例,请看一看!我检查了您更新的示例@yogen,但我得到了相同的NullPointerException错误。@如果cacheManager
已被模拟,则suraz在执行前断言或调试。。另外,请确保您有@RunWith(MockitoRunner.class)
因为这告诉我要启用模拟注释。如果仍然出现相同的错误,请告诉我我给你的代码与你的代码有什么不同。感谢你的快速响应。这有助于我找到解决方案。实际上,我必须模拟每一个步骤才能通过测试。下面我发布了解决方案,希望它可能是某个人。谢谢你或者您响应。第行下面应该返回缓存,而不是emptyList Mockito.when(cacheManager.getCache(Matchers.anyString())。然后返回(Collections.emptyList());此外,我还在从第行获取NullPointerExcetpion(Cache.Entry:cacheManager.getCache(“人”){
@ActiveProfiles("test")
@RunWith(MockitoJUnitRunner.class)
@Slf4j
public class CacheTest {
@Spy
@InjectMocks
PersonService personService;
@Mock
private CacheManager cacheManager;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void fallbackMCCsWithNonEmptyCache() {
List<Person> persons = Collections.singletonList(new Person());
Iterator<Cache.Entry <Object, Object>> cacheIterator =
Mockito.mock(Iterator.class);
Cache <Object, Object> cache = Mockito.mock(Cache.class);
Cache.Entry <Object, Object> entry = Mockito.mock(Cache.Entry.class);
Mockito.when(cacheManager.getCache(Mockito.anyString()))
.thenReturn(cache);
Mockito.when(cache.iterator()).thenReturn(cacheIterator);
List<Person> personList = personService.fallbackPersons();
Assert.assertNotNull(personList);
Assert.assertTrue(personList.isEmpty());
}
}