Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit assertEquals() not working when comparing two objects

I'm trying to get the hang of Java. Unit testing is pretty important to me and so recently I've started to use JUnit. It was tough to begin with but I'm getting the hang of it. All of my tests have worked up to this point, with the exception of comparing two objects of the same class (I haven't tried testing a function that creates an object of a different class). Basically, when I have a method within a class that creates a new instance of the class, and I try to test the method, I get a weird error.

"expected:runnersLog.MTLog@433c675d but was runnersLog.MTLog@3f91beef"

I have tried researching this problem, but haven't found anything of much help. Here's the link to my classes on github. The method I am trying to test is the mt() method, and the test class is ILogTest.

This is not the only case where I am having this problem. With any class that has a method that returns a new object of the same class, I am getting this exact same 3f91beef error (even when the object is more complicated - with arguments)

like image 772
David Anekstein Avatar asked Jun 24 '15 17:06

David Anekstein


People also ask

Can assertEquals compare two objects?

assertEquals. Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown. If expected and actual are null , they are considered equal.

Does assertEquals work with objects?

Yes, it calls equals and there is a separate method, assertSame , that uses == . Just to clear things up, assertEquals works with any object since all objects declare equals .

How do you check for equality of two objects in Junit?

assertEquals() calls equals() on your objects, and there is no way around that. What you can do is to implement something like public boolean like(MyClass b) in your class, in which you would compare whatever you want. Then, you could check the result using assertTrue(a. like(b)) .

How do you check assertEquals in Junit?

The assertSame() method tests if two object references point to the same object. The assertNotSame() method tests if two object references do not point to the same object. void assertArrayEquals(expectedArray, resultArray); The assertArrayEquals() method will test whether two arrays are equal to each other.


2 Answers

You need to overrride equals, the equals method in the superclass Object checks for references if both references point to the same object equals is true if not false, so you need to write down an equals method that will check your objects content and check if the values are the same, it is also recommended that you override your hashCode method too.

An example could be:

Custom a= new Custom("");
Custom b= a;

//b would be equal a. because they reference the same object.
Custom c= new Custom("");
//c would not be equal to a, although the value is the same.

to learn more you could check: Why do I need to override the equals and hashCode methods in Java?

like image 59
DguezTorresEmmanuel Avatar answered Oct 27 '22 01:10

DguezTorresEmmanuel


assertEquals will use Object#equals for each object being compared. Looks like your class ILogTest doesn't override equals method, so calling Object#equals will just compare the references by itself, and since they're different object references, the result will be false.

You have two options:

  1. Override public boolean equals(Object o) in ILogTest.
  2. Use assertEquals on the relevant fields that implement equals method e.g. String, Integer, Long, etc. This one requires more code but is useful when you cannot modify the class(es) being asserted.
like image 42
Luiggi Mendoza Avatar answered Oct 27 '22 00:10

Luiggi Mendoza