需要帮助编写CQ5.4应用程序的JUnit测试用例

时间:2012-10-26 14:51:26

标签: java-ee junit cq5 sling

我正在使用JMockito和sling commons测试api为CQ5应用程序编写JUnit测试用例。我能够模拟Request参数。我的实际java方法有一些逻辑迭代Request参数map例如requestParamMapObj.keySet()。iterator()并得到一些节点属性。但是当我运行Unit测试用例时,所述keySet迭代器没有被执行并抛出目标调用异常甚至requestParamMapObj都有数据。

这是我第一次为内容管理应用程序编写JUnit测试用例。

任何帮助都非常值得赞赏。

这是我的测试方法代码:

this.context.checking(new Expectations() {
    { 
        this.one(mockRequest).getRequestParameterMap();
        this.will(returnValue(paramsMap)); 
    } 
}); 

当Junit到达下面的代码时,它会抛出目标调用异常。

private Map<String, String> getAllAnswers(final RequestParameterMap reqParamMap) { 
    final Iterator<String> keySetIterator1 = reqParamMap.keySet().iterator();
}

2 个答案:

答案 0 :(得分:0)

查看完整代码会很有帮助,但是根据你列出的内容和你的意见到目前为止,听起来你可能需要设置对调用keySet()方法时会发生什么的期望。我怀疑也许你有一个模拟了ParameterMap对象,但可能还没有设置对该ParameterMap的期望,这样当调用它的keySet方法时它会知道该做什么/返回什么。再一次,我只是在猜测,但我希望这会有所帮助。

答案 1 :(得分:0)

是肖恩。你是对的,当我设置对keySet()方法的期望时,它被调用。 this.allowing(paramsMap).keySet(); 但我的问题是,我们需要为每一种行为设定期望。这是否有意义,我的意思是单元测试代码变得超过实际的java代码:)。 我们是否有任何替代解决方案,以便我们可以减少测试代码。

            this.allowing(resource).adaptTo(Node.class);
            this.will(returnValue(node));

            this.allowing(node).hasNode("voting");
            this.will(returnValue(true));

            allowing(repository).loginAdministrative(null);
            will(returnValue(session));

            allowing(session).getWorkspace();
            will(returnValue(workspace));

            allowing(workspace).getQueryManager();
            will(returnValue(queryManager));
相关问题