diff --git a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java index 3768cd1747..64fe404f5e 100644 --- a/spring-data-mongodb/src/main/java/org/baeldung/model/User.java +++ b/spring-data-mongodb/src/main/java/org/baeldung/model/User.java @@ -1,7 +1,5 @@ package org.baeldung.model; -import java.util.Calendar; - import org.baeldung.annotation.CascadeSave; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.annotation.Id; @@ -80,6 +78,11 @@ public class User { } public Integer getYearOfBirth() { - return Calendar.getInstance().get(Calendar.YEAR) - age; + return yearOfBirth; } + + public void setYearOfBirth(Integer yearOfBirth) { + this.yearOfBirth = yearOfBirth; + } + } diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java index febfb0181f..6082743bda 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateQueryIntegrationTest.java @@ -186,8 +186,9 @@ public class MongoTemplateQueryIntegrationTest { public void whenSavingUser_thenYearOfBirthIsCalculated() { final User user = new User(); user.setName("Alex"); + user.setYearOfBirth(1985); mongoTemplate.insert(user); - assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getYearOfBirth(), is(2015)); + assertThat(user.getYearOfBirth(), is(1985)); } }