EqualsBuilder.reflectionEquals se basa en la implementación igual de cada campo

I wanted to use reflectionEquals for some of my tests but found that it only works when the classes of all non null fields implements equals. This is not the case for some of my fields. I thought about using a subclass with that overrides equals and uses explicitly uses reflectionEquals on the field that does not implement equals (headers). This works but is messy and wanted to check if there's a better way. The reason identity equals does not work in my tests is I'm using gson to marshall objects to a database. Here's my subclass solution:

    public boolean equals(Object obj) {
        TrackingEvent other = (TrackingEvent) obj;
        return EqualsBuilder.reflectionEquals(this.getHeaders(), other.getHeaders()) && 
                EqualsBuilder.reflectionEquals(this, other, new String[] {"headers"});

preguntado el 03 de enero de 13 a las 18:01

The simplest solution to this is to write a traditional equals implementation by hand operating on the fields you want to operate on. -

Yes, of course if I relied on equals in production code I would implement it, but I don't and want a zero maintenance equals for test code. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.