我在嘲笑一个模拟物体的地图时遇到了一些麻烦。
我正在使用Mockito。
这是我的功能:
@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException, IOException {
try {
Session session = request.getResourceResolver().adaptTo(Session.class);
PrintWriter writer = response.getWriter();
boolean testsPassed = isValid(agentMgr.getAgents().values()) && testAgents(agentMgr.getAgents(), session);
if (testsPassed) {
writer.write(successOutput);
} else {
writer.write(failureOutput);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private boolean isValid(Map<String, Agent> agents) {
for (Map.Entry<String, Agent> agentEntry : agents.entrySet()) {
if (agentEntry != null) {
Agent agent = agentEntry.getValue();
boolean isValid = agent.isValid();
String id = agent.getId();
// regex match string id
if (id.matches(regex) || id.equalsIgnoreCase(goldId)) {
if (agent != null) {
if (!agent.isValid()) {
return false;
}
}
}
}
}
return true;
}
我试图像这样组装我的模拟对象:
when(passAgent1.isValid()).thenReturn(true);
when(passAgent1.getId()).thenReturn("publish");
Map<String, Agent> mockAgents = new HashMap<String, Agent>();
mockAgents.put("publish", passAgent1);
mockAgents.put("i-12345678", passAgent2);
when(agentMgr.getAgents()).thenReturn(mockAgents);
当我运行时,似乎Agent对象作为代理返回$$ EnhancerWithMockitoByGLIB $$ 738eb07c
isValid和getId的模拟函数都返回null,因此我的测试失败了。
非常感谢任何建议。