意外的方法调用easymock

时间:2014-07-21 11:38:44

标签: java junit easymock

我的Spring MVC控制器中有这样的方法

@RequestMapping(method = RequestMethod.POST)
public ModelAndView send(@RequestParam(value = "t", required = false) String t,
        @RequestParam(value = "s", required = false) String s,
        @RequestParam(value = "p", required = false) String p,
        @RequestParam(value = "c", required = false) String c,
        @RequestParam(value = "o", required = false) String o,
        @RequestParam(value = "s", required = false) String s,
        @RequestParam(value = "to", required = false) String to,
        @RequestParam(value = "cc", required = false) String cc,
        @RequestParam(value = "bcc", required = false) String bcc,
        @RequestParam(value = "url", required = false) String url,
        @RequestParam(value = "pd", required = false) String pd) {

    RRS status = validate(new PV(t), new PV(to),
            new PV(o), new PV(s));
    if (status != null) {
        return response(status.getCode(), status.getMessage());
    }

    NM nM = buildMessage(t, s, p,
            c, o, s, to, cc, bcc, url, pd);
    service.store(nM);

    nP.send(nM.getUid());

    return response(nM.getUid());
}

我想测试这部分

    RRS status = validate(new PV(t), new PV(to),
            new PV(o), new PV(s));
    if (status != null) {
        return response(status.getCode(), status.getMessage());
    }

但是当我创建这样的测试方法时

@Test
public void testSendMethod() {
    NC controller = createMock(NC.class);
    PV validator = createMock(PV.class);
    expect(controller.validate(validator)).andReturn(null);
    expect(validator.isValid()).andReturn(true);
    replay(validator);
    replay(controller);

    controller.send(T, S, P, C, O, S, TO, CC,
            BCC, URL, PD);
    verify(validator);
    verify(controller);
}

我收到了错误

Unexpected method call NC.send("TValue", "SValue" .... /*my parameters values*/ ):
NC.validate(EasyMock for class PV): expected: 1, actual: 0

方法validate看起来像这样

 protected RRS validate(INV... validations) {
    RRS resp = null;
    for (IN validation : validations) {
        if (!validation.isValid()) {
            resp = validation.errorResponse();
            break;
        }
    }
    return resp;
}

问题是什么?我以为我设置方法调用的顺序是正确的,返回值也应该没问题......

0 个答案:

没有答案