Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试模拟映射时未返回模拟对象_Java_Mockito - Fatal编程技术网

Java 尝试模拟映射时未返回模拟对象

Java 尝试模拟映射时未返回模拟对象,java,mockito,Java,Mockito,我在模拟模拟模拟对象的地图时遇到了一些问题 我用的是Mockito 以下是我的功能: @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { try { Session session = request.getResourceResolver()

我在模拟模拟模拟对象的地图时遇到了一些问题

我用的是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;
}
@覆盖
受保护的无效数据集(SlingHttpServletRequest请求,
SlingHttpServletResponse响应)引发ServletException,IOException{
试一试{
Session Session=request.getResourceResolver().adapto(Session.class);
PrintWriter=response.getWriter();
布尔testsPassed=isValid(agentMgr.getAgents().values())&&testAgents(agentMgr.getAgents(),session);
如果(已测试){
writer.write(成功输出);
}否则{
writer.write(失败输出);
}
}捕捉(中断异常e){
e、 printStackTrace();
}
}
私有布尔值是有效的(映射代理){
对于(Map.Entry-agentry:agents.entrySet()){
if(agentEntry!=null){
Agent=agentEntry.getValue();
布尔值isValid=agent.isValid();
String id=agent.getId();
//正则表达式匹配字符串id
if(id.matches(regex)| | id.equalsIgnoreCase(goldId)){
如果(代理!=null){
如果(!agent.isValid()){
返回false;
}
}
}
}
}
返回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);
when(passAgent1.isValid())。然后返回(true);
当(passAgent1.getId())。然后返回(“发布”);
Map mockAgents=newhashmap();
mockAgents.put(“publish”,passAgent1);
mockAgents.put(“i-12345678”,passAgent2);
当(agentMgr.getAgents())。然后返回(mockAgents);
运行时,代理对象似乎以代理$$EnhancerWithMockitoByGLIB$$738eb07c的形式返回

isValid和getId的模拟函数都返回null,因此,我的测试失败


任何建议都将不胜感激。

因此您删掉了两种
passAgent1
方法。您是否还存根了
passAgent2
的任何方法?如果没有,为什么没有?没有复制并粘贴它。是的,我确实在实际的应用程序中存根了它们。你说的“模仿
agents.values()
”是什么意思?请不要告诉我你是在模仿
Map
界面!在发布所有代码之前,很难判断您做了什么和没有做什么。但是你真的不想模拟
Map
本身-只需要使用一个充满模拟对象的常规映射。不要模拟映射。模仿地图是个坏主意。对您来说,最好的办法可能是尝试使用调试器来找出您做错了什么。或者(第三次也是最后一次)你可以试着把你所有的代码都发布在这里,而不仅仅是你认为有用的部分。嘿,大卫,谢谢你的建议,不要嘲笑地图。有两个问题。首先,我意外地将两个when语句应用于同一个函数处理程序,然后使用Map-mock沿着花园小路走去。都分类好了。再次感谢你的帮助。