如何处理单元测试中缺少的分支

时间:2014-10-12 20:30:32

标签: java unit-testing

我正在尝试用Java完成一项任务,而且我有一段时间让我的被覆盖的equals方法正常工作。

我有一个项目类,它接受2个构造函数中的一个,第一个接受2个值,第二个接受4.我的equals方法仅适用于使用4参数构造函数创建的Item。我不确定如何处理只有4个参数中有2个的Items

这就是我所拥有的:

  1. 我从测试null开始,两者都属于同一类。
  2. 如果好的话,我会转到Item对象进行更具体的测试。
  3. 如果我的编码是正确的,那么我应该测试两个构造函数使用的2个值。但是,如果对象包含其他两个参数,我不确定我是否正在测试此权限。
  4. 使用4参数Item时,我的单位测试等于通过,但是失败时带有2参数Item

    我在这里和其他地方搜索过(我通常在这里找到我的答案,从来没有问过问题),但没有什么能帮助我解决这个具体问题。由于我从未提出过问题,请告诉我您需要的其他信息/代码。

    @Override
    public boolean equals(final Object theOther) {
        boolean result = false;
        if ((theOther != null) && (theOther.getClass() == this.getClass())) {
            final Item otherItem = (Item) theOther;
            if (otherItem.myItemName.equals(this.myItemName)
                && otherItem.myItemPrice.equals(this.myItemPrice)) { 
                if (otherItem.myItemBulkQuantity != 0 
                                && !otherItem.myItemBulkPrice.equals(BigDecimal.ZERO)) {
                    result = otherItem.myItemBulkPrice.equals(this.myItemBulkPrice)
                        && otherItem.myItemBulkQuantity == this.myItemBulkQuantity;                   
                } 
            }
        }
    
        return result;
    }
    

    以下是我的单元测试:

    /**
     * Test method for bulk quantities.
     */
    @Test
    public void testEqualsObjectWithBulkQuantities() {
        myItem = new Item(ITEM_TEST_NAME, new BigDecimal(ITEM_TEST_PRICE), 6, 
                          new BigDecimal(ITEM_TEST_BULK_PRICE));
        final Item thisNextItem = new Item(ITEM_TEST_NAME, new BigDecimal(ITEM_TEST_PRICE), 6, 
                                   new BigDecimal(ITEM_TEST_BULK_PRICE));
        assertEquals(true, myItem.equals(thisNextItem));
    }
    
    /**
     * Test method for equals method with no bulk quantity.
     */
    @Test
    public void testEqualsObjectNoBulkQuantities() {
        myItem = new Item(ITEM_TEST_NAME, new BigDecimal(ITEM_TEST_PRICE));
        final Item thisNextItem = new Item(ITEM_TEST_NAME, new BigDecimal(ITEM_TEST_PRICE));
        assertEquals(true, myItem.equals(thisNextItem));
    }
    

    下面的构造函数代码
    public Item(final String theName, final BigDecimal thePrice) {
    
        myItemName = theName;
    
        myItemPrice = thePrice;
    
        myItemBulkPrice = BigDecimal.ZERO;
    
        myItemBulkQuantity = 0;
    
    }
    
    /**
     * Creates a new item with 4 parameters.
     * 
     * @param theName The name of the item.
     * @param thePrice The price of the item.
     * @param theBulkQuantity The bulk quantity of the item.
     * @param theBulkPrice The price of the item using a bulk quantity.
     */
    public Item(final String theName, final BigDecimal thePrice, final int theBulkQuantity,
                final BigDecimal theBulkPrice) {
    
        myItemName = theName;
    
        myItemPrice = thePrice;
    
        myItemBulkQuantity = theBulkQuantity;
    
        myItemBulkPrice = theBulkPrice;
    }
    

2 个答案:

答案 0 :(得分:0)

您的问题如下:

if (otherItem.myItemName.equals(this.myItemName)
        && otherItem.myItemPrice.equals(this.myItemPrice)) { 
        if (otherItem.myItemBulkQuantity != 0 
                        && !otherItem.myItemBulkPrice.equals(BigDecimal.ZERO)) {
            result = otherItem.myItemBulkPrice.equals(this.myItemBulkPrice)
                && otherItem.myItemBulkQuantity == this.myItemBulkQuantity;                   
        } 
}

如果两个参数相等,但其他两个参数不相同,则不会设置结果,并用false初始化它,因此它将返回false!你必须在第二个if上实现else,并设置类似

的东西
result = otherItem.myItemName.equals(this.myItemName)
            && otherItem.myItemPrice.equals(this.myItemPrice)

或直接result = true,因为你在第一个if中检查了它!

答案 1 :(得分:0)

请原谅我,这并没有真正回答这个问题,但我担心今晚我不能说好if金字塔。

在没有ifs的情况下链接elses就像布尔&&

 if ( this is true )
   // AND
   if ( this is true )
     thisIsApplied()

这导致更易读/易理解/易记录/快速无遗忘 - 可检查/可调试/ ......:

return
  theOther != null && 
  theOther.getClass() == getClass() &&

  // check names' and prices' equality
  otherItem.myItemName.equals( myItemName ) &&
  otherItem.myItemPrice.equals( myItemPrice ) &&

  // check bulk quantities and prices for not being zero       
  otherItem.myItemBulkQuantity != 0 &&
  !otherItem.myItemBulkPrice.equals( BigDecimal.ZERO ) &&

  // check bulk quantities and prices for equality 
  otherItem.myItemBulkQuantity == myItemBulkQuantity &&
  otherItem.myItemBulkPrice.equals( myItemBulkPrice );
相关问题