object java AssertEquals当compareTo显示它们相等时,同样不返回true

时间:2016-08-25 18:58:19

标签: java junit

这显示了具有要引用的属性的类 https://stackoverflow.com/posts/comments/65623190?noredirect=1

Product.class

@Override
public int compareTo(Goods o) {
    int compared = 0;
    System.out.println("Date " + o.getDate() + " " + this.getDate());
    if(o.getDate() != this.getDate()) {
        compared = 1;
    }
    System.out.println("Price " + o.getPrice() + " " + this.getPrice());
    if(Double.compare(o.getPrice(), this.getPrice()) != 0) {
        compared = 1;
    }
    System.out.println("Status " + o.getStatus() + " " + this.getStatus());
    if(o.getStatus() != this.getStatus()) {
        compared = 1;
    }
    System.out.println("Name " + o.getProductName() + " " + this.getProductName());
    if(o.getProductName() != this.getProductName()) {
        compared = 1;
    }

        if(o.getDate().equals(this.getDate()) &&
                o.getStatus().equals(this.getStatus()) &&
                o.getProductName().equals(this.getProductName()) &&
                o.getPrice().equals(this.getPrice())) {
            compared = 0;
        }
    return compared;
}

JunitTest.class

@Test
public Test {
    Product product = new Product();    
    product.setProductName("Product_A");
    product.setPrice(4.10);
    product.setStatus(Library.STATUSES.THEM);
    calendar.set(Calendar.MONTH, Calendar.AUGUST);
    date = calendar.getTime();
    product.setDate(date);

    actualGoods.add(product);
    product = new Product();
    product.setProductName("Product_A");
    product.setPrice(3.70);
    product.setDate(date);
    product.setStatus(Library.STATUSES.HIM);
    expectedGoods.add(product);

    assertSame(expectedGoods, classLib.ProductUpdate(actualGoods, Library.STATUSES.HIM));
 }

然后我的方法取代了更便宜的产品,并将状态转换为我的图书馆类中的“HIM”状态

public List<Product> redLinePromotionListStatusUpdate(List<Product> actualProduct, STATUSES status) {
    List<Product> currentProducts = new ArrayList<>();
    List<Product> originalStatusProducts = new ArrayList<>();
    int listIndex = 0;

    if(actualProduct != null) {
        for (Product product : actualProduct) {
            if (product.getStatus().toString().equals(STATUSES.THEM.toString())) {
                originalStatusProducts.add(listIndex, product);
                listIndex++;
            }
            if (product.getStatus().equals(STATUSES.valueOf(status.toString())) && containsName(originalStatusProducts, product.getProductName())) {
                if ((callReductionPrice(product.getPrice(), originalStatusProducts.get(originalStatusProducts.size() - 1).getPrice())) < originalStatusProducts.get(originalStatusProducts.size() - 1).getPrice()) {
                    currentProducts.add(product);
                }
            }
        }
    }
    return currentProducts;
}

我得知我的两个对象并不相同。当我使用类方法compareTo时,我得到它们就是在某个地方我的Library类方法中失去了等价。

修改

java.lang.AssertionError:expected:&lt; [Goods @ 45322aea]&gt;但是:&lt; [Goods @ 149fac29]&gt; java.lang.AssertionError:期望的相同:&lt; [Goods @ 34c636a7]&gt;不是:&lt; [Goods @ 59797c7]&gt;

当我尝试assertTrue(object.equals(testObject))时,我得到了java.lang.AssertionError 的修改     调试预期Sun Aug 21 16:19:10 EDT 2016价格3.7状态HIM  名称Product_A  实际产品_A HIM 3.7 Sun 8月21日16:19:10 EDT 2016

Product_A REDLINE30 3.7来自预期对象的null  Product_A REDLINE30 3.7来自实际对象的null

但我仍然得到AssertionError。 你知道如何从断言失败中获取对象的更多信息细节吗?

1 个答案:

答案 0 :(得分:2)

Assert.assertSame doc说:

Asserts that two objects refer to the same object.

两个对象可以相等(object1.equals(obejct2)),而不是相同的参考。