假设以下模型
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;
}
}
}
所以我根据以下内容开发了(在上面的代码之前)测试驱动开发:
obs:对于每个测试方法,dataProvider(通过使用 TestNG)提供参数
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);
}
}
如果我首先开发了失败案例,并对每个测试方法进行了模拟,然后在模拟之后进行了实现,这是否是测试驱动开发方法?
问候,
最佳答案
虽然人们可能会对其施加其他条件,但通常如果您在编码之前和编码时编写测试,那么它可能是测试驱动的。您通常会提出一开始会失败的测试,然后编写代码使它们通过。
关于java - 它是测试驱动开发方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392191/