Java 如何在Groovy中模拟String.equals?

Java 如何在Groovy中模拟String.equals?,java,groovy,spock,Java,Groovy,Spock,出于测试目的,我需要重写“equals”方法: def any = [equals: { true }] as String any == 'should be true' // false 有关问题的更多详细信息: class EmployeeEndpointSpec extends RestSpecification { void "test employee" () { when: get "/v1/employee", paramete

出于测试目的,我需要重写“equals”方法:

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: "jimmy@fakemail.com",
                    dateCreated:"2015-01-01T01:01:00.000", // this value should be ignored
                    lastUpdated: "2015-01-01T01:01:00.000" // and this
                ]
            ]
    }
}
class EmployeeEndpointSpec扩展了RestSpecification{
作废“测试员工”(){
什么时候:
获取“/v1/employee”参数
然后:
expectedStatus.equals(response.statusCode)
expectedJson.equals(response.json)
哪里:

参数如果不需要比较提到的字段,请删除它们:

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: "jimmy@fakemail.com",
                    dateCreated:"2015-01-01T01:01:00.000",
                    lastUpdated: "2015-01-01T01:01:00.000"
                ]
            ]
    }
}
如果您不喜欢最后两行,可以将其替换为:

json.keySet().contains('lastUpdated', 'dateCreated')
答案是:

String.metaClass.equals = { Object _ -> true }

请解释为什么需要模拟这种低级方法。可能有更好的方法,例如模拟获取字符串的任何内容。我需要它来测试REST响应(JSON转换为Map)。我的任务是检查返回的响应与预期的相同,除了一些值-它们应该被忽略。响应示例:
[suces:“true”,date:“2015-01-01”]
,我想要这样的东西
[suces:“true”,date:new AnyValue]。等于(响应)
。编写单元测试的代码,并解释要测试什么,而不是如何测试。因此,不要检查这些值……使用类似Hamcrest的方法。我认为没有理由在这种情况下进行模拟。但如果JSON是嵌套的,会怎么样?从嵌套映射中删除这些字段将是非常困难的tedious@AndreyT,在上面的示例中,JSON不是嵌套的。请提供这是一个真实的例子,因为我似乎试图解决不正确的问题。这是一个真实的例子,但我也尝试为未来的测试找到答案。谢谢。过早优化是万恶之源。如果这个答案让你满意,请接受或投票。过一会儿我会添加一个首选的解决方案。其他答案和评论很有用,但是谢谢你回答他真正的问题。
String.metaClass.equals = { Object _ -> true }