为了进行测试,我需要覆盖“等于”方法:
def any = [equals: { true }] as String
any == 'should be true'
// false
有关问题的更多详细信息:
class EmployeeEndpointSpec extends RestSpecification {
void "test employee" () {
when:
get "/v1/employee", parameters
then:
expectedStatus.equals(response.statusCode)
expectedJson.equals(response.json)
where:
parameters << [
[:],
[id: 824633720833, style: "small"]
]
expectedStatus << [
HttpStatus.BAD_REQUEST,
HttpStatus.OK
]
expectedJson << [
[errorCode: "badRequest"],
[
id: 824633720833,
name: "Jimmy",
email: "[email protected]",
dateCreated:"2015-01-01T01:01:00.000", // this value should be ignored
lastUpdated: "2015-01-01T01:01:00.000" // and this
]
]
}
}
lastUpdated
和dateCreated
可能会随时间变化,我需要以某种方式忽略它们。
最佳答案
如果不需要比较提到的字段,请删除它们:
class EmployeeEndpointSpec extends RestSpecification {
void "test employee" () {
when:
get "/v1/employee", parameters
then:
expectedStatus.equals(response.statusCode)
def json = response.json
json.remove('dateCreated')
json.remove('lastUpdated')
expectedJson.equals(response.json)
where:
parameters << [
[:],
[id: 824633720833, style: "small"]
]
expectedStatus << [
HttpStatus.BAD_REQUEST,
HttpStatus.OK
]
expectedJson << [
[errorCode: "badRequest"],
[
id: 824633720833,
name: "Jimmy",
email: "[email protected]",
dateCreated:"2015-01-01T01:01:00.000",
lastUpdated: "2015-01-01T01:01:00.000"
]
]
}
}
我还将分别测试消极和积极的情况。
您也可以与测试键值分开测试
keySet()
,而不是比较整个映射。这是我要这样做的方式:then:
def json = response.json
json.id == 824633720833
json.name == "Jimmy"
json.email == "[email protected]"
json.dateCreated.matches('<PATTERN>')
json.lastUpdated.matches('<PATTERN>')
如果您不喜欢最后两行,可以将其替换为:
json.keySet().contains('lastUpdated', 'dateCreated')
关于java - 如何在Groovy中模拟String.equals?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28042074/