简体   繁体   English

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

[英]object java AssertEquals Same not returning true when compareTo shows they're equal

This shows the class with attributes to reference https://stackoverflow.com/posts/comments/65623190?noredirect=1 这显示了具有引用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 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));
 }

Then I have my method that replaces the cheaper product and moves the status into the 'HIM' status in my Library class 然后,我有了替换廉价产品的方法,并在Library类中将其状态更改为“ 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;
}

Some how I am getting that my two objects are not equal. 我如何得知我的两个对象不相等。 When I use the class method compareTo I get that they are so it's losing equvilence in my Library class method somewhere. 当我使用类方法compareTo时,我知道它们是正确的,因此它在我的类库方法中失去了对等性。

EDIT 编辑

java.lang.AssertionError: expected:<[Goods@45322aea]> but was:<[Goods@149fac29]> java.lang.AssertionError: expected same:<[Goods@34c636a7]> was not:<[Goods@59797c7]> java.lang.AssertionError:预期:<[Goods @ 45322aea]>,但为:<[Goods @ 149fac29]> java.lang.AssertionError:预期相同:<[Goods @ 34c636a7]>不是:<[Goods @ 59797c7] >

When I try assertTrue(object.equals(testObject)) I get java.lang.AssertionError EDIT Debugging expected Sun Aug 21 16:19:10 EDT 2016 price 3.7 status HIM name Product_A actual Product_A HIM 3.7 Sun Aug 21 16:19:10 EDT 2016 当我尝试使用assertTrue(object.equals(testObject))时,我得到java.lang.AssertionError EDIT调试预期的星期日8月21日16:19:10 EDT 2016价格3.7状态HIM名称Product_A实际的Product_A HIM 3.7 Sun 8月21日16:19:10美国东部夏令时间2016

Product_A REDLINE30 3.7 null from the expected object Product_A REDLINE30 3.7 null from the actual object 预期对象的Product_A REDLINE30 3.7为空实际对象的Product_A REDLINE30 3.7为空

But I still get the AssertionError. 但是我仍然得到AssertionError。 Do you know how I can get more information details of the Object from an assertion failure? 您知道我如何从断言失败中获取对象的更多信息吗?

Assert.assertSame doc says: Assert.assertSame文档说:

Asserts that two objects refer to the same object.

Two objects can be equal ( object1.equals(obejct2) ) without being the same reference. 两个对象可以相等( object1.equals(obejct2) ),而不必是相同的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM