我使用PowerMock遇到了一个问题,如果我想只返回一次模拟项目,我觉得我不得不创建丑陋的代码。 作为一个例子,我有以下代码:
mockMap = spy(new HashMap());
HashMap<String, String> normalMap = new HashMap<>();
HashMap<String, String> normalMap2 = new HashMap<>();
HashMap<String, String> normalMap3 = new HashMap<>();
whenNew(HashMap.class).withNoArguments()
.thenReturn(normalMap)
.thenReturn(mockMap)
.thenReturn(normalMap2)
.thenReturn(normalMap3);
这当然有效,但感觉非常笨重,特别是因为我需要为每个新的调用创建一个新的hashmap。
所以我的问题是:有没有办法告诉PowerMock它应该在一定数量的电话后停止干扰?
修改 看完答案后,我得到了以下内容:
final AtomicInteger count = new AtomicInteger(0);
whenNew(HashMap.class).withNoArguments().thenAnswer(invocation -> {
switch (count.incrementAndGet())
{
case 1:
return mockMap;
default:
return new HashMap<String, String>();
}
});
但是它使用以下代码给我一个StackOverFlowError:
describe("test", () -> {
beforeEach(() -> {
mockStatic(HashMap.class);
final AtomicInteger count = new AtomicInteger(0);
whenNew(HashMap.class).withNoArguments().thenAnswer(invocation ->
{
switch (count.incrementAndGet())
{
case 5:
return mockMap;
default:
return new HashMap<String, String>();
}
});
});
it("Crashes on getting a new HashMap", () -> {
HashMap map = new HashMap<>();
map.put("test", "test");
HashMap normal = new HashMap<String, String>();
expect(normal.containsValue("test")).toBeTrue();
});
});
值得注意的是,在我的大型测试中,我没有一个mockStatic(HashMap.class)可以获得相同的错误(如果我删除了mockStatic调用,我会摆脱它)
有效的解决方案(但感觉就像一种解决方法)正在通过将默认语句编辑为:
来构建default:
normalMap.clear();
return normalMap;
答案 0 :(得分:2)
您可以将thenReturn与多个参数一起使用,如下所示:
whenNew(HashMap.class).withNoArguments()
.thenReturn(normalMap, mockMap, normalMap2, normalMap3);
或者像这样编写自己的Answer:
whenNew(HashMap.withNoArguments()).doAnswer(new Answer() {
private int count = 0;
public Object answer(InvocationOnMock invocation) {
// implement your logic
// if (count ==0) etc.
}
});
答案 1 :(得分:1)
您可以使用Mockito的org.mockito.stubbing.Answer
:
final AtomicInteger count = new AtomicInteger(0);
PowerMockito.whenNew(HashMap.class).withNoArguments()
.thenAnswer(new Answer<HashMap<String, String>>() {
@Override
public HashMap<String, String> answer(InvocationOnMock invocation) throws Throwable {
count.incrementAndGet();
switch (count.get()) {
case 1: // first call, return normalMap
return normalMap;
case 2: // second call, return mockMap
return mockMap;
case 3: // third call, return normalMap2
return normalMap2;
default: // forth call (and all calls after that), return normalMap3
return normalMap3;
}
}
});
请注意,我必须使用java.util.concurrent.atomic.AtomicInteger
并将其声明为final
变量,原因有两个:
final int
,则无法count++
注意:在此解决方案中,您还必须将所有normalMap
更改为final
实际上,如果你的所有normalMap
都相同,你可以这样做:
PowerMockito.whenNew(HashMap.class).withNoArguments()
.thenAnswer(new Answer<HashMap<String, String>>() {
@Override
public HashMap<String, String> answer(InvocationOnMock invocation) throws Throwable {
count.incrementAndGet();
if (count.get() == 2) { // second call
return mockMap;
}
return normalMap; // don't forget to make normalMap "final"
// or if you prefer: return new HashMap<String, String>();
}
});
PS :如this answer中所述,您也可以在匿名类中创建一个AtomicInteger
计数器,而不是使用int
(由您自己决定)选择,因为两者都有效):
PowerMockito.whenNew(HashMap.class).withNoArguments()
.thenAnswer(new Answer<HashMap<String, String>>() {
private int count = 0;
@Override
public HashMap<String, String> answer(InvocationOnMock invocation) throws Throwable {
count++;
if (count == 2) { // second call
return mockMap;
}
return normalMap; // don't forget to make normalMap "final"
// or if you prefer: return new HashMap<String, String>();
}
});
这也适用于switch
解决方案。