EasyMock - 对模拟对象的期望

时间:2011-06-26 05:29:11

标签: easymock

我是EasyMock的新手。我正在尝试为我的Spring WS端点编写一个EasyMock测试并继续运行到一个问题。详情如下:

端点:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "UserCreate")<BR>
public void handleUserCreationRequest(@RequestPayload Element userCreate) throws JDOMException {

        String userName = userNameExp.valueOf(userCreate);
        String loginName = userLoginNameExp.valueOf(userCreate);
        String eMail = eMailExp.valueOf(userCreate);
        String region = regionExp.valueOf(userCreate);
        String department = departmentExp.valueOf(userCreate);
        String businessUnit = businessUnitExp.valueOf(userCreate);

        userManagementService.userCreate(userName, loginName, eMail, 
                region, department, businessUnit);
    }

测试:

@Before<BR>
    public void setUp() throws JDOMException {<BR>
        xPath = createNiceMock(XPath.class);<BR>
        payload = createNiceMock(Element.class);<BR>
        managementService = createStrictMock(UserManagementService.class);<BR>

        serviceEndpoint = new UserManagementServiceEndpoint(managementService);
    }
@Test
    public void testUserCreationHandler() throws JDOMException {

        expect(xPath.valueOf(payload)).andReturn("userName");
        expect(xPath.valueOf(payload)).andReturn("loginName");
        expect(xPath.valueOf(payload)).andReturn("eMail");
        expect(xPath.valueOf(payload)).andReturn("region");
        expect(xPath.valueOf(payload)).andReturn("department");
        expect(xPath.valueOf(payload)).andReturn("businessUnit");
        managementService.userCreate("userName", "loginName", "eMail", 
                "region", "department", "businessUnit");
        expectLastCall();
        replayAll();

        serviceEndpoint.handleUserCreationRequest(payload);
        verifyAll();
    }

错误讯息:

Failed tests:
  testUserCreationHandler(com.xxx.usermanagement.endpoint.UserManagementServiceEndpoint
Test):
  Expectation failure on verify:
    valueOf(EasyMock for class org.jdom.Element): expected: 6, actual: 0

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0<BR><BR>

如果有人能帮助我,我将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您遇到的问题是您的XPath模拟对象未设置为UserManagementServiceEndpoint对象。 您应该修改构造函数以接受XPath参数或为其创建一个setter。