简体   繁体   English

Mockito `when` 在真实物体上

[英]Mockito `when` on real object

I want to mock Dao class when object contains specific values.当对象包含特定值时,我想模拟 Dao 类。 For example, I want to mock call to the database when userlist.getStatus == UserlistStatus.DONE例如,我想在userlist.getStatus == UserlistStatus.DONE时模拟对数据库的调用

If attached the code snippet.如果附上代码片段。

my application code is as follow:我的应用程序代码如下:

UserlistBo userlistBo = userlistDao.getByIdAndPublisherId(userlistId, publisher.getId());
if (userlistBo.getStatus() == UserlistStatus.SOURCE_IN_PROGRESS && userlistStatusDto.getStatus().equalsIgnoreCase(UserlistStatus.DONE.getValue())) {
    userlistBo.setStatus(UserlistStatus.DONE); // 1
    userlistBo = userlistDao.save(userlistBo); // 3
}

userlistBo object at marker 1 is as follow :标记 1 处的 userlistBo 对象如下:

result = {UserlistBo@1610} "UserlistBo{id=1, name='ul-test-99-9999', status=DONE, source=SEGMENT, ruleId=1, publisherId=1, filename=filename.csv, createdById=null, lastModifiedById=null, creationTimestamp=null, lastModifiedTimestamp=null}"

and my test code looks like :我的测试代码如下:

expectedUserlistBo.setId(1);
expectedUserlistBo.setName("ul-test-99-9999");
expectedUserlistBo.setPublisherId(1);
expectedUserlistBo.setRuleId(1);
expectedUserlistBo.setFilename("filename.csv");
expectedUserlistBo.setSource(UserlistSource.SEGMENT);
expectedUserlistBo.setStatus(UserlistStatus.SOURCE_IN_PROGRESS);
when(userlistDao.getByIdAndPublisherId(1, publisherBo.getId())).thenReturn(expectedUserlistBo);


UserlistBo expectedUserlistStatusDoneBo = new UserlistBo();
CopyHelperBean.copy(expectedUserlistStatusDoneBo, expectedUserlistBo);
expectedUserlistStatusDoneBo.setStatus(UserlistStatus.DONE); // 2
when(userlistDao.save(expectedUserlistStatusDoneBo)).thenReturn(expectedUserlistStatusDoneBo); // 4

userlistBo at marker 2 is :标记 2 处的 userlistBo 是:

result = {UserlistBo@1514} "UserlistBo{id=1, name='ul-test-99-9999', status=DONE, source=SEGMENT, ruleId=1, publisherId=1, filename=filename.csv, createdById=null, lastModifiedById=null, creationTimestamp=null, lastModifiedTimestamp=null}"

Since these two are different objects with the same values, when test is executed at marker 3, userlistBo is returned as null .由于这两个是具有相同值的不同对象,因此在标记 3 处执行 test 时, userlistBo 返回为null

How should I mock at marker 4, so when Dao is called at marker 3, it will return me expectedUserlistStatusDoneBo instead of null ?我应该如何模拟标记 4,所以当在标记 3 处调用 Dao 时,它会返回我expectedUserlistStatusDoneBo而不是null

Object in application code is应用程序代码中的对象是

result = {UserlistBo@1610} "UserlistBo{id=1, name='ul-test-99-9999', status=DONE, source=SEGMENT, ruleId=1, publisherId=1, filename=filename.csv, createdById=null, lastModifiedById=null, creationTimestamp=null, lastModifiedTimestamp=null}"

Dummy object created in test code is在测试代​​码中创建的虚拟对象是

result = {UserlistBo@1514} "UserlistBo{id=1, name='ul-test-99-9999', status=DONE, source=SEGMENT, ruleId=1, publisherId=1, filename=filename.csv, createdById=null, lastModifiedById=null, creationTimestamp=null, lastModifiedTimestamp=null}"

though the values are same, these two are different objects虽然值相同,但这两个是不同的对象

when(userlistDao.getByIdAndPublisherId(1, publisherBo.getId())).thenReturn(expectedUserlistBo);

in when condition, equality of above to object fails.when条件下,上面与对象的相等性失败。 To Solve this, Added equals() and hashcode() in UserlistBo object.为了解决这个问题,在 UserlistBo 对象中添加了 equals() 和 hashcode()。

@xerx593 thanks for pointing out @xerx593 感谢您指出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM