有了 PricingEngine
并定义了应用程序规则之后,可能渴望验证所有东西都工作正确。现在问题就变成,用 JUnit 还是 FIT?为什么不两者都用呢?通过 JUnit 测试所有组合是可能的,但是要进行许多编码。最好是用 JUnit 测试少数几个值,迅速地验证代码在工作,然后依靠 FIT 的力量运行想要的组合。请看看当我这么尝试时发生了什么,从清单 9 开始:
清单 9. JUnit 迅速地验证了代码在工作
package org.acme.store.discount.engine.junit; import junit.framework.TestCase; import org.acme.store.Money; import org.acme.store.discount.engine.PricingEngine; import org.acme.store.discount.engine.ProductType; import org.acme.store.discount.engine.WholesaleOrder; public class DiscountEngineTest extends TestCase { public void testCalculateDiscount() throws Exception{ WholesaleOrder order = new WholesaleOrder(); order.setNumberOfCases(20); order.setPricePerCase(new Money(10.00)); order.setProductType(ProductType.YEAR_ROUND); PricingEngine.applyDiscount(order); assertEquals(0.05, order.getDiscount(), 0.0); } public void testCalculateDiscountNone() throws Exception{ WholesaleOrder order = new WholesaleOrder(); order.setNumberOfCases(20); order.setPricePerCase(new Money(10.00)); order.setProductType(ProductType.SEASONAL); PricingEngine.applyDiscount(order); assertEquals(0.0, order.getDiscount(), 0.0); } } |