Reputation: 431
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?
Upvotes: 1
Views: 695
Reputation: 3070
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())
Upvotes: 3
Reputation: 717
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.
Upvotes: 1
Reputation: 3894
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);
Upvotes: 2