Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
JUnit4 introduced @Test annotations and removes the need to prefix test method names with ‘test’. Take advantage of this and use a different test case naming convention to test the class behaviour, not it’s implementation
Compare the two units test below and see which one is easier to read.
Think about diagnosing a test failure and also how quickly you can understand the what the code does.
public class ShoppingCartTest {
private ShoppingCart cart = null;
@Before
public void init() {
cart = new ShoppingCart();
cart.addItem( new
ShoppingCart.Item( "Chocolate bar", 52 ) );
}
@Test
public void testProcessPayment() {
assertTrue( cart.processPayment( new CashPayment() ) );
assertTrue( cart.processPayment( new VoucherPayment( "123" ) ) );
assertFalse( cart.processPayment( new VoucherPayment( null ) ) );
assertFalse( cart.processPayment( new VoucherPayment( "" ) ) );
}
}
This test suite tests the expected behaviour and uses descriptive test names. Each test sticks to a single concept. This is also a great example of Unit Tests as documentation, each test case tells a story of what the code does.
public class ShoppingCartTest {
private ShoppingCart cart = null;
@Before
public void init() {
cart = new ShoppingCart();
cart.addItem( new ShoppingCart.Item( "Chocolate bar", 52 ) );
}
@Test
public void shouldProcessCashPayment() {
assertTrue( cart.processPayment( new CashPayment() ) );
}
@Test
public void shouldProcessVoucherPayment() {
assertTrue( cart.processPayment( new VoucherPayment( "123" ) ) );
}
@Test
public void shouldNotProcessVoucherPaymentForInvalidVoucherReference()
{
assertFalse( cart.processPayment( new VoucherPayment( null ) ) );
assertFalse( cart.processPayment( new VoucherPayment( "" ) ) );
}
}