[英]How to mock abstract method of an abstract class?
I have a below method in which I am using this line ProcBOF.getInstance().find
to get some data.我有一个下面的方法,我使用这一行
ProcBOF.getInstance().find
来获取一些数据。
private static Map<Long, Long> getAll(Map<Long, Event> holder) throws FinderException {
Map<Long, Long> map = new HashMap<>();
if (holder.isEmpty()) {
return map;
}
Set<Long> items = holder.keySet();
long[] itemIds = Longs.toArray(items);
List<TeraBo> teraBos = TBOF.getInstance().findItemIdsIn(itemIds);
for (TeraBo tBo : teraBos) {
ProcessBo bo = ProcBOF.getInstance().find(tBo, holder.get(tBo.getItem().getId()).getDataId(), ReadSet.FULL, null);
bo.getVar();
map.put(tBo.getItem().getId(), bo.getVar());
}
return map;
}
Now I want to mock the find
method of ProcBOF
class so that it can return some dummy ProcessBo
object from which we can call getVar()
method but the issue is ProcBOF
is an abstract class and find
is an abstract method so I am not able to understand how to mock this abstract method of abstract class.现在我想模拟
ProcBOF
class 的find
方法,以便它可以返回一些虚拟ProcessBo
object ,我们可以从中调用getVar()
方法,但问题是ProcBOF
是一个抽象的find
了解如何模拟抽象 class 的这个抽象方法。
public interface ProcessBo extends BOI {
//...
}
public abstract class ProcBOF extends BaseBof {
//...
public abstract ProcessBo find(TeraBo saleBo, long dataId, ReadSet readSet, Filter filter) throws FinderException;
}
You're implementation it's a little weird, so I'm not sure If I was able to reproduce it corretly (a getInstance() method on an abstract class is weird).你的实现有点奇怪,所以我不确定我是否能够正确地重现它(抽象 class 上的 getInstance() 方法很奇怪)。
But, I think that with something like this it should work:但是,我认为这样的事情应该可以工作:
import mockit.Expectations;
import mockit.Mocked;
import mockit.Tested;
import mockit.integration.junit4.JMockit;
import org.junit.runner.RunWith;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@RunWith(JMockit.class)
public class Test {
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
/**Tested class*/
@Tested
private Foo foo;
/**Mocked so that all instances are a mock*/
@Mocked
private ProcBOF procBOF;
/**Mocked so that all instances are a mock*/
@Mocked
private ProcessBo processBo;
// Constructors --------------------------------------------------
// Static --------------------------------------------------------
// Public --------------------------------------------------------
@org.junit.Test
public void name() throws Exception {
// GIVEN
new Expectations() {{
// we mock the getInstance method and return always the mocked instance
ProcBOF.getInstance();
result = procBOF;
// we mock the find method and return the mocked process Bo
procBOF.find((TeraBo) any, 2L, ReadSet.FULL, null);
result = processBo;
// we also mock the getVar method
processBo.getVar();
result = "foo";
}};
// test data
Map<Long, Event> map = new HashMap<>();
map.put(1L, new Event());
// WHEN
// call the getAll method
Map<Long, Object> ret = foo.getAll(map);
// THEN
// we get the mocked value
assertThat(ret.get(1L), is("foo"));
}
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.