In the book “Implementing Domain Driven Design” of Vaughn Vernon, in the Testing of Value Objects section, the author talks about how we can use just shallow copy instead of deep copy to test the mutability of an object.
This is the constructor of BusinessPriority
public final class BusinessPriority implements Serializable {
private static final long serialVersionUID = 1L;
private BusinessPriorityRatings ratings;
public BusinessPriority(BusinessPriorityRatings aRatings) {
super();
this.setRatings(aRatings);
}
public BusinessPriority(BusinessPriority aBusinessPriority) {
this(aBusinessPriority.ratings());
}
This is the testcase
public void testCostPercentageCalculation() throws Exception {
BusinessPriority businessPriority = new BusinessPriority(
new BusinessPriorityRatings(2, 4, 1, 1));
BusinessPriority businessPriorityCopy =
new BusinessPriority(businessPriority);
assertEquals(businessPriority, businessPriorityCopy);
BusinessPriorityTotals totals = new BusinessPriorityTotals(53, 49, 53 + 49, 37, 33);
float cost = businessPriority.costPercentage(totals);
assertEquals(this.oneDecimal().format(cost), "2.7");
assertEquals(businessPriority, businessPriorityCopy);
}
As I can see, shallow copying is not enough in this case, because shallow copying will make businessPriority.rating() and businessPriorityCopy.rating() refer to the same object, thus always equal regardless of whether some attribute of this object is changed or not
So am I correct and if so, why the book talks about shallow copying in this case ?
/////////////////////