O'Reilly logo

Test Driven Development: By Example by Kent Beck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. Privacy

$5 + 10 CHF = $10 if rate is 2:1$5 * 2 = $10Make amount privateDollar side effects?Money rounding?equals()hashCode()Equal nullEqual object

Now that we have defined equality, we can use it to make our tests more “speaking.” Conceptually, the operation Dollar.times() should return a Dollar whose value is the value of the receiver times the multiplier. Our test doesn't exactly say that:

public void testMultiplication() {
					Dollar five= new Dollar(5);
					Dollar product= five.times(2);
					assertEquals(10, product.amount);
					product= five.times(3);
					assertEquals(15, product.amount);
					}
				

We can rewrite the first assertion to compare Dollars to Dollars:

public void testMultiplication() {
					Dollar five= new Dollar(5);
					Dollar product= ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required