[JAVA-1905] Bson instances returned from Filters, Updates, and Aggregates methods should implement equals() or hashCode() Created: 27/Jul/15  Updated: 16/Jun/19  Resolved: 08/Feb/19

Status: Closed
Project: Java Driver
Component/s: Builders
Affects Version/s: 3.0.2
Fix Version/s: 3.11.0

Type: Improvement Priority: Major - P3
Reporter: Blake Bauman Assignee: John Stewart (Inactive)
Resolution: Done Votes: 4
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by JAVA-2092 Add equals() and toString() to Updates Closed

 Description   

Filter implementations in com.mongodb.client.model.Filters from the 3.0.X Java driver do not implement the equals() method. This is causing great difficulty unit testing the data layer since you can't compare a Filter Bson object with an expected value.



 Comments   
Comment by Githook User [ 08/Feb/19 ]

Author:

{'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}

Message: Implement equals and hashCode methods for Bson instances
returned from Filters, Aggregates, Indexes, Sorts and Updates

JAVA-1905
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/fc8b272e144265d736036c52902479a9b86e2053

Comment by Przemyslaw Wojnowski [ 22/Nov/17 ]

This is really annoying issue. One needs to use Document(...) instead, because it can be compared in unit tests.

Comment by Blake Bauman [ 25/Apr/16 ]

Currently, to get around this issue, we're using Mockito with ArgumentCaptor objects. We capture the arg, then take the expected Bson and the actual Bson, convert both to JSON strings using a codec registry (since toString() doesn't work,either), then compare the resulting strings to validate.

It ends up being quite a big pain to do this for all the tests.

Comment by Jeffrey Yemin [ 25/Apr/16 ]

Added Updates and Aggregates to this issue, in addition to Filters.

Comment by Blake Bauman [ 27/Jul/15 ]

While that's technically true, all of the collections in java.util.Collections would have the same problem. All it would need to guarantee is that equals() returns true for its values.

The problem is, we're not trying to call assertEquals() on the Bson document, we're using mock objects. The mock object frameworks rely on equals() to know when a particular method has been invoked with specific arguments. Something like:

    final Bson bson = Filters.eq("a", "b");
    when(myMock.someMethod(bson)).thenReturn("foo");
    verify(myMock).someMethod(bson);

Comment by Jeffrey Yemin [ 27/Jul/15 ]

There is no way to properly implement equals directly on the Bson instances returned from the Filters factory methods because there is no way to ensure that all the values contained in the filter itself implement equals. But there is an easy workaround: since BsonDocument implements equals, and the Bson interface has a toBsonDocument method, you can do something like this:

     CodecRegistry codecRegistry = ...
     Bson filter = Filters.eq("field", ...);
     assertEquals(BsonDocument.parse("{"field : ...}"), filter.toBsonDocument(Document.class, codeRegistry));

This is exactly how we test the correctness of the Filters class itself.

Generated at Thu Feb 08 08:55:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.