它是一种测试驱动开发方法吗?

时间:2009-09-08 05:43:21

标签: java tdd

假设以下模型

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);
    }
}

如果我首先开发了失败案例,并且对于每个测试方法都是模拟,并且在模拟实现后,它是一种测试驱动开发方法吗?

的问候,

3 个答案:

答案 0 :(得分:4)

虽然人们可能会有其他条件,但通常如果您在编写测试之前和编写测试时,它可能是测试驱动的。您通常会在最初尝试失败的测试,然后编写代码以使它们通过。

答案 1 :(得分:3)

只要有测试,您就无法查看代码并查看它是否是使用TDD开发的。 TDD周期如下所示:

  1. 写测试。
  2. 让它通过。
  3. 重构。
  4. 一个微妙的事情是你可能不会添加任何新的功能,除非它是一个失败的测试通过。

    使用的模拟数量无关紧要。

答案 2 :(得分:3)

谁在调用successAllowedOrder(),successPartiallyAllowedOrder(),successDeniedOrder()等方法?我问的原因TestCases通常没有参数。

通常使用TDD编写的单元测试遵循triple A pattern

  • A rrange
  • A ct
  • A ssert

还有一些灯具,即通过setup()和tearDown()方法执行,创建和清理测试的环境。夹具对于同一类中的所有测试都是通用的。

使用您的Product类,这将提供以下内容:

public class TestOrder extends TestCase
{
    // assume the fixtures creates the Product instances used for the tests here

    void TestValidOrder() 
    {
      // arrange - create an order
      // act - do something on the order - could be empty if testing the creation
      // assert - test the status of the order
    } 

}

设计点:为什么计算allowedQuantity和requiredQuantity的循环不属于Order类?

看一下bowling game episode,这对TDD来说真是一个很棒的教程。 最后,你可以在不使用模拟对象的情况下进行TDD。