gpt4 book ai didi

java - EasyMock:将对象与其克隆进行比较时出现断言错误

转载 作者:行者123 更新时间:2023-12-01 10:57:29 25 4
gpt4 key购买 nike

我收到这样的断言错误:

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 克隆时,才会发生断言错误。我怎样才能避免这个问题?

最佳答案

遵循您的示例有点困难,但我最好的猜测是您需要使用数组相等 argument matcher而不是默认的匹配器,它检查 .equals(),对于数组来说不太有用。

尝试将您的期望更改为:

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

关于java - EasyMock:将对象与其克隆进行比较时出现断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33552104/

25 4 0
文章推荐: java - Linkedhashmap<字符串,对象>
文章推荐: block - 移除 EPiServer 7 Shared Block 周围生成的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com