Hi,
Suppose the following model
public class Product {
private Integer requiredQuantity;
private Integer allowedQuantity;
// getters and setters
}
public class Order {
public void allowsOrder(List<Product> productList) throws AppException {
Integer allowedQuantity = 0;
Integer requiredQuantity = 0;
for(Product product: productList) {
if(product.getAllowedQuantity().compareTo().product.getRequiredQuantity() > 0)
throw new AllowedQuantityIsGreaterThanRequiredQuantity();
allowedQuantity += product.getAllowedQuantity();
requiredQuantity += product.getRequiredQuantity();
}
switch(allowedQuantity.compareTo(requiredQuantity)) {
case 0:
setOrderStatus(OrderStatus.ALLOWED);
break;
case -1:
if(allowedQuantity.equals(0))
setOrderStatus(OrderStatus.DENIED);
else
setOrderStatus(OrderStatus.PARTIALLY_ALLOWED);
break;
}
}
}
So i have developed (BEFORE code above) a Test Driven Development according to:
obs: for each test method a dataProvider (by using TestNG) supplies parameters
public void successAllowedOrder(List<Product> productList, Order beforeAllowingOrderMock, Order afterAllowingOrderMock) {
Integer allowedQuantity = 0;
Integer requiredQuantity = 0;
for(Product product: productList) {
allowedQuantity += product.getAllowedQuantity();
requiredQuantity += product.getRequiredQuantity();
}
assertEquals(allowedQuantity, requiredQuantity);
assertNull(beforeAllowingOrderMock.getOrderStatus());
assertEquals(OrderStatus.ALLOWED, afterAllowingOrderMock.getOrderStatus());
// beforeAllowingOrderMock is now a implementation
beforeAllowingOrderMock = new Order();
beforeAllowingOrderMock.allowsOrder(productList);
assertNotNull(beforeAllowingOrderMock.getOrderStatus());
assertEquals(beforeAllowingOrderMock.getOrderStatus(), afterAllowingOrderMock.getOrderStatus());
}
public void successPartiallyAllowedOrder(List<Product> productList, Order beforeAllowingOrderMock, Order afterAllowingOrderMock) {
Integer allowedQuantity = 0;
Integer requiredQuantity = 0;
for(Product product: productList) {
allowedQuantity += product.getAllowedQuantity();
requiredQuantity += product.getRequiredQuantity();
}
assertTrue(requiredQuantity > allowedQuantity);
assertNull(beforeAllowingOrderMock.getOrderStatus());
assertEquals(OrderStatus.PARTIALLY_ALLOWED, afterAllowingOrderMock.getOrderStatus());
// beforeAllowingOrderMock is now a implementation
beforeAllowingOrderMock = new Order();
beforeAllowingOrderMock.allowsOrder(productList);
assertNotNull(beforeAllowingOrderMock.getOrderStatus());
assertEquals(beforeAllowingOrderMock.getOrderStatus(), afterAllowingOrderMock.getOrderStatus());
}
public void successDeniedOrder(List<Product> productList, Order beforeAllowingOrderMock, Order afterAllowingOrderMock) {
Integer allowedQuantity = 0;
Integer requiredQuantity = 0;
for(Product product: productList) {
allowedQuantity += product.getAllowedQuantity();
requiredQuantity += product.getRequiredQuantity();
}
assertTrue(allowedQuantity == 0);
assertNull(beforeAllowingOrderMock.getOrderStatus());
assertEquals(OrderStatus.DENIED, afterAllowingOrderMock.getOrderStatus());
// beforeAllowingOrderMock is now a implementation
beforeAllowingOrderMock = new Order();
beforeAllowingOrderMock.allowsOrder(productList);
assertNotNull(beforeAllowingOrderMock.getOrderStatus());
assertEquals(beforeAllowingOrderMock.getOrderStatus(), afterAllowingOrderMock.getOrderStatus());
}
public void failureAllowedQuantityIsGreaterThanRequiredQuantity(List<Product> productList, Order beforeAllowingOrderMock) {
Integer allowedQuantity = 0;
Integer requiredQuantity = 0;
for(Product product: productList) {
allowedQuantity += product.getAllowedQuantity();
requiredQuantity += product.getRequiredQuantity();
}
assertTrue(allowedQuantity > requiredQuantity);
try {
beforeAllowingOrderMock.allowsOrder(productList);
} catch(Exception e) {
assertTrue(e instanceof AllowedQuantityIsGreaterThanRequiredQuantity);
}
// beforeAllowingOrderMock is now a implementation
beforeAllowingOrderMock = new Order();
try {
beforeAllowingOrderMock.allowsOrder(productList);
} catch(Exception e) {
assertTrue(e instanceof AllowedQuantityIsGreaterThanRequiredQuantity);
}
}
If i have developed failure case first and for each test method a mock and, after mock, a implementation, is it a Test Driven Development approach ?
regards,