While putting together a presentation, I have a situation in which I expect an exception, but I am getting none, when I run my corresponding unit test. What I am doing is incrementally modifying a bean. In this version of the Product
and Accessory
classes, I have removed the setters/getters for all the properties (save for a setter for one of the Product properties). I have previously converted my classes to use field access notation. So, since I have removed the setters/getters, I am expecting an exception because the field visibility modifiers are private.
Here is the Accessory class:
public class Accessory {
private String name
private BigDecimal cost
private BigDecimal price
public Accessory() {
this.cost = BigDecimal.ZERO
this.price = BigDecimal.ZERO
}
public Accessory(String name, BigDecimal cost, BigDecimal price) {
this.name = name
this.cost = cost
this.price = price
}
@Override
public int hashCode() {
final int prime = 31
int result = 1
result = prime * result + ((cost == null) ? 0 : cost.hashCode())
result = prime * result + ((name == null) ? 0 : name.hashCode())
result = prime * result + ((price == null) ? 0 : price.hashCode())
return result
}
public boolean equals(Accessory obj) {
return name == obj.name &&
cost == obj.cost &&
price == obj.price
}
@Override
public String toString() {
return "Accessory [" + "name=" + name + ", cost=" + cost + ", price=" + price + "]"
}
}
Here are snippets from the Product class:
public class Product {
private String model
private List<Accessory> accessories
private TreeMap<Integer, BigDecimal> priceBreaks
private BigDecimal cost
private BigDecimal price
...
public BigDecimal getAccessorizedCost() {
...
for (Accessory pkg : this.accessories) {
pkgCost = pkgCost.add pkg.cost
}
return pkgCost
}
...
}
I would expect that the line pkgCost = pkgCost.add pkg.cost
in the above snippet would throw an exception. Likewise, I would think the following asserts in my unit test would do the same:
@Test public void canCreateDefaultInstance() {
assertNull "Default construction of class ${defaultProduct.class.name} failed to properly initialize model.", defaultProduct.model
assertTrue "Default construction of class ${defaultProduct.class.name} failed to properly initialize accessories.", defaultProduct.accessories.isEmpty()
assertTrue "Default construction of class ${defaultProduct.class.name} failed to properly initialize priceBreaks.", defaultProduct.priceBreaks.isEmpty()
assertEquals "Default construction of class ${defaultProduct.class.name} failed to properly initialize cost.", BigDecimal.ZERO, defaultProduct.cost as BigDecimal
assertEquals "Default construction of class ${defaultProduct.class.name} failed to properly initialize price.", BigDecimal.ZERO, defaultProduct.price as BigDecimal
}
Here are the metaclass properties and methods:
MetaClass Properties are:
[accessorizedCost, accessorizedPrice, class, priceBreaks]
MetaClass Methods are:
[__$swapInit, addPriceBreak, calcDiscountMultiplierFor, calcVolumePriceFor, equals, getAccessorizedCost, getAccessorizedPrice, getClass, getMetaClass, getProperty, hashCode, invokeMethod, notify, notifyAll, setMetaClass, setPriceBreaks, setProperty, toString, wait]
You can see, for example, that there are no properties nor corresponding getter/setters for the privately defined model
, accessories
, cost
and price
fields. So, like the line in the Product class not failing when referencing the cost property of Accessory, I do not understand how the unit tests can pass when there is not property nor getter/setters for these private fields.
I am compiling using Groovy 2.0.4 and running Eclipse.
What am I missing or not understanding?