1

After running a Junit test for user-defined object serialization, it was failed and gave me the results

Expected: com.me.Position@7a92922

Actual: com.me.Position@25618e91

I have defined the following class

public class Position {

    private double x;
    private double y;

    /**
     * default constructor
     */
    public Position() {
    }

    /**
     * paramterized constructor
     * 
     * @param x
     *          x-coordinate
     * @param y
     *          y-coordinate
     */
    public Position(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
}

then I defined another class to serialize and deserialize a Position object which is an instance of the previous class as following

public class PositionSerializer {

    static void serialize(Position position, OutputStream outputStream) {
        OutputStreamUtil.serializeDouble(position.getX(), outputStream);
        OutputStreamUtil.serializeDouble(position.getY(), outputStream);
    }

    static Position deserialize(InputStream inputStream) {
        double x = InputStreamUtil.deserializeDouble(inputStream);
        double y = InputStreamUtil.deserializeDouble(inputStream);
        Position positionObject = new Position();
        positionObject.setX(x);
        positionObject.setY(y);
        return positionObject;
    }
}

Finally, I wrote a unit test as follows

public class PositionSerializerTest {

    private InputStream iStream;
    private ByteArrayOutputStream oStream;

    @Before
    public void init() {
        oStream = new ByteArrayOutputStream();
    }

    Position serialzeAndDeserializeObject(Position positionObject) {
        PositionSerializer.serialize(positionObject, oStream);
        iStream = new ByteArrayInputStream(oStream.toByteArray());
        return PositionSerializer.deserialize(iStream);
    }

    @Test
    public void equals_equal() {
        Position positionObject = new Position(5.5, 10.5);
        Position deserializedPosition = serialzeAndDeserializeObject(positionObject);
        assertThat(deserializedPosition).isEqualTo(positionObject);
    }
}

what was wrong? and how to fix it?

Salem Masoud
  • 411
  • 11
  • 32
  • Possible duplicate of [What issues should be considered when overriding equals and hashCode in Java?](https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) – Progman Jul 31 '18 at 16:41

3 Answers3

2

There is nothing wrong,but you are creating a new instance in deserialize method:

Position positionObject = new Position();

This will always call new instance of Position Object and hence you can not compare it using == operator

You should override equals method as below:

public boolean equals(Object o) {
    if (o == this) {
        return true;
    }
    if (!(o instanceof Position)) {
        return false;
    }
    Position otherObject = (Position)o;
    if(this.x == otherObject.x && this.y == otherObject.y)
        return true;
    else return false;
}

And then call :

assertThat(deserializedPosition).isEqualTo(positionObject);
Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
2

You are checking reference equality which is not equal because your deserialize method returns new instance on every call, use below for comparing values :

assertThat(deserializedPosition.getX()).isEqualTo(positionObject.getX())    
assertThat(deserializedPosition.getY()).isEqualTo(positionObject.getY())
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Emre Savcı
  • 3,034
  • 2
  • 16
  • 25
1

It looks like your test is comparing the object references and not the object values. Override the equals function or compare each value in the position object separately.

gkgkgkgk
  • 707
  • 2
  • 7
  • 26