【问题标题】:EasyMock: AssertionError on comparing object with its cloneEasyMock:将对象与其克隆进行比较时出现 AssertionError
【发布时间】:2015-11-05 18:26:55
【问题描述】:

我收到了这样的 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 克隆时才会出现断言错误。 我该如何规避呢?

【问题讨论】:

    标签: java testng easymock


    【解决方案1】:

    按照你的例子有点难,但我最好的猜测是你需要使用数组相等 argument matcher 而不是默认的匹配器,它检查 .equals(),对数组不太有用。

    尝试将您的期望更改为:

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

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多