EasyMock:将对象与其克隆进行比较时的AssertionError

时间:2015-11-05 18:26:55

标签: java testng easymock

我遇到了这样的AssertionError:

java.lang.AssertionError: 
  Unexpected method call ICustomerDAO.getVersionStamps(-1, ["CustomerData", "UserData"], EasyMock for interface java.sql.Connection):
    ICustomerDAO.getVersionStamps(-1, ["CustomerData", "UserData"], EasyMock for interface java.sql.Connection): expected: 1, actual: 0
    at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:44)
    at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
    at com.sun.proxy.$Proxy3.getVersionStamps(Unknown Source)
    at com.mgmt.ConfigClient.getRequestedVersions(ConfigClient.java:200)
    at com.mgmt.ConfigClientTest.testGetRequestedVersions(ConfigClientTest.java:329)

这是测试代码:

@Test
public void testGetRequestedVersions() {
    ConfigClient client = new ConfigClient();
    client.dao = EasyMock.createStrictMock(ICustomerDAO.class);
    Connection connection = EasyMock.createStrictMock(Connection.class);
    long[] versions = {34, 12};
    EasyMock.expect(client.dao.getVersionStamps(-1, ConfigFilesRegenTask.getRequestedVersions(), connection)).andReturn(versions);
    EasyMock.replay(client.dao);
    ConfigToken token = client.getRequestedVersions(connection);
    EasyMock.verify(client.dao);
    assertEquals("Wrong customer version", versions[0], token.getCustomerVersion());
}

ConfigClient类:

public ConfigToken getRequestedVersions(Connection connection) {
    final ConfigToken token = new ConfigToken(-1, -1);
    long[] requestedVersions = dao.getVersionStamps( -1, ConfigFilesRegenTask.getRequestedVersions(), connection);
    token.setCustomerVersion(requestedVersions[0]);
    return token;
}

ConfigFilesRegenTask类包含get方法,该方法返回现有数组的克隆

public static String[] getRequestedVersions() {
    return REQUESTED_VERSIONS.clone();
}

仅当我返回数组的REQUESTED_VERSIONS克隆时才会发生断言错误。 我怎么能绕过这个?

1 个答案:

答案 0 :(得分:1)

有点难以理解你的例子,但我最好的猜测是你需要使用Array Equality argument matcher而不是默认的匹配器来检查.equals(),这对于阵列。

尝试将您的期望改为:

EasyMock.expect(client.dao.getVersionStamps(eq(-1), aryEq(ConfigFilesRegenTask.getRequestedVersions()), eq(connection))).andReturn(versions);