我有一个单元测试,在这里我模拟一个服务类来保存一个域。最初,我的控制器方法如下所示:
def save(Long organizationId, Convention convention) {
conventionService.save(organizationId, convention)
if (convention.hasErrors()) {
response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
respond convention.errors
} else {
response.status = HttpStatus.CREATED.value()
respond convention
}
}一般来说,这是因为Java是通过引用传递的,因此传递到保存方法的convention在整个方法中都是相同的convention对象。但是,当模拟conventionService.save方法时,通过引用传递不起作用。调整我的方法以说明这一点:
def save(Long organizationId, Convention convention) {
convention = conventionService.save(organizationId, convention)
if (convention.hasErrors()) {
response.status = HttpStatus.UNPROCESSABLE_ENTITY.value()
respond convention.errors
} else {
response.status = HttpStatus.CREATED.value()
respond convention
}
}允许我的测试通过,因为convention对象是我期望从模拟中得到的:
1 * service.save(1, _) >> new Convention(
id: 1,
name: 'Con 1',
description: 'This is a pretty cool convention, everyone should go',
startDate: new Date(),
endDate: new Date()+10,
organization: organization)我的问题是,这是我应该报告的预期行为还是错误?
发布于 2016-11-06 20:56:18
我的问题是,这是我应该报告的预期行为还是错误?
这是预期的行为。这不是你应该报告的bug。
https://stackoverflow.com/questions/40454347
复制相似问题