Skip to content

Commit 2f15c79

Browse files
committed
Test null as a value of ObjectId
1 parent f810311 commit 2f15c79

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/integrationTest/java/com/mongodb/hibernate/type/ObjectIdIntegrationTests.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818

1919
import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME;
2020
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
2122

2223
import com.mongodb.client.MongoCollection;
2324
import com.mongodb.hibernate.junit.InjectMongoCollection;
2425
import com.mongodb.hibernate.junit.MongoExtension;
2526
import jakarta.persistence.Entity;
2627
import jakarta.persistence.Id;
2728
import jakarta.persistence.Table;
29+
import java.util.Objects;
2830
import org.bson.BsonDocument;
2931
import org.bson.BsonInt32;
32+
import org.bson.BsonNull;
3033
import org.bson.BsonObjectId;
3134
import org.bson.types.ObjectId;
3235
import org.hibernate.testing.orm.junit.DomainModel;
@@ -54,7 +57,8 @@ void insert() {
5457
assertThat(mongoCollection.find())
5558
.containsExactly(new BsonDocument()
5659
.append(ID_FIELD_NAME, new BsonInt32(1))
57-
.append("v", new BsonObjectId(item.v)));
60+
.append("v", new BsonObjectId(item.v))
61+
.append("vNull", BsonNull.VALUE));
5862
}
5963

6064
@Test
@@ -64,10 +68,7 @@ void getById() {
6468
item.v = new ObjectId(2, 0);
6569
sessionFactoryScope.inTransaction(session -> session.persist(item));
6670
var loadedItem = sessionFactoryScope.fromTransaction(session -> session.get(Item.class, item.id));
67-
assertThat(loadedItem)
68-
.usingRecursiveComparison()
69-
.withStrictTypeChecking()
70-
.isEqualTo(item);
71+
assertEquals(item, loadedItem);
7172
}
7273

7374
@Override
@@ -82,5 +83,20 @@ static class Item {
8283
int id;
8384

8485
ObjectId v;
86+
ObjectId vNull;
87+
88+
@Override
89+
public boolean equals(Object o) {
90+
if (o == null || getClass() != o.getClass()) {
91+
return false;
92+
}
93+
Item item = (Item) o;
94+
return id == item.id && Objects.equals(v, item.v) && vNull == item.vNull;
95+
}
96+
97+
@Override
98+
public int hashCode() {
99+
return Objects.hash(id, v, vNull);
100+
}
85101
}
86102
}

0 commit comments

Comments
 (0)