-1

我正在比较这两个对象,但预期对象不等于实际对象。但它们具有相同的值。请提供一些帮助。

为什么 ?

public testJsonToObject() {    
     Hello expected = new Hello();
     String json = "{\"id\":5,\"name\":\"Family\",\"deleteable\
 ":\"false\"}";    
     Hello actual = (Hello) mapper.readValue(json, Hello.class);
     System.out.println("Family " + actual);
     expected.setId(5);
     expected.setName("Family");
     expected.setDeleteable(false);
     System.out.println(expected);
     Assert.assertEqual(expected, actual);
  }

简单的波乔 Class Hello

public class Hello {

 private int id;
 private String name;    
 private boolean deleteable;     


  /* Getter and Setters*/

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public boolean isDeleteable() {
  return deleteable;
 }
 public void setDeleteable(boolean deleteable) {
  this.deleteable = deleteable;
 }

}

为什么这没有给我预期的结果

4

2 回答 2

0

尝试这个....

if ( (expected.getId == actual.getId) && (expected.getName.equals(actual.getName))&& (expected.isDeleteable == actual.isDeleteable) ) { 
            System.out.println( "OK" );
        }

代替...

if ( expected.equals( actual ) ) {  // Reference Checking not the value actually .
        System.out.println( "OK" );
    }

if ( expected.equals( actual ) )这里的equals方法属于对象类,它实际上检查引用而不是包装的对象/变量值。

于 2016-02-29T10:46:33.670 回答
0

您可以尝试实施Comparable以允许通过以下方式进行比较:

expected.compareTo(actual);

或者

compare(expected, actual);
于 2016-02-29T11:02:01.860 回答