Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two objects using assertEquals or by any other way in JUNIT testing?

Below is JUNIT test which I have written to compare the object created (Actual) from the Json string and the object created (Expected) within the test function.

    @Test
    public void testSalesChannel_1_ObjSrc(){

    SalesChannel oScExpected = new SalesChannel();
    oScExpected.setSalesChannelId(79662);
    oScExpected.setCompanyName("BMW");
    oScExpected.setCountry("DE");
    oScExpected.setActive(true);

    String jsonStringActual = "{\"salesChannelId\":79662,"
            + "\"companyName\":\"BMW\","
            + "\"country\":\"DE\",\"isActive\":true,"
            + "\"salesChannelContact\":[]}";
    SalesChannel oScActual = gson.fromJson(jsonStringActual, SalesChannel.class);
    System.out.println(oScExpected.toString());
    System.out.println(oScActual.toString());
    //assertEquals(oScExpected, oScActual); 
    assertTrue(oScExpected.equals(oScActual));      
}

BUT when I execute assertEquals(), it fails the test. What could be the reason?

AND my Sales Channel class is:

package com.pf.activationServer;

import java.util.List;

import com.pf.activationServer.SalesChannelContact;

public class SalesChannel {

private int salesChannelId;
private String companyName;
private CountryCode country;
private boolean isActive;
private List<SalesChannelContact> salesChannelContact;

// getter methods
protected int getSalesChannelId() {
    return salesChannelId;
}
protected String getCompanyName() {
    return companyName;
}
protected CountryCode getCountry() {
    return country;
}
protected boolean isActive() {
    return isActive;
}
protected List<SalesChannelContact> getSalesChannelContact() {
    return salesChannelContact;
}

// setter methods
protected void setSalesChannelId(int salesChannelId) {
    this.salesChannelId = salesChannelId;
}
protected void setCompanyName(String companyName) {
    this.companyName = companyName;
}
protected void setCountry(String a){
    this.country = CountryCode.valueOf(a);
}
protected void setActive(boolean isActive) {
    this.isActive = isActive;
}
protected void setSalesChannelContact(List<SalesChannelContact> salesChannelContact) {
    this.salesChannelContact = salesChannelContact;
}

// Checks whether two SalesChannel objects are equal or not

public boolean equals(SalesChannel other) {
    if (this == other)
        return true;
    if (other == null)
        return false;
    if (getClass() != other.getClass())
        return false;
    //SalesChannel other = (SalesChannel) obj;
    if (companyName == null) {
        if (other.companyName != null)
            return false;
    } else if (!companyName.equals(other.companyName))
        return false;
    if (country != other.country)
        return false;
    if (isActive != other.isActive)
        return false;
    if (this.salesChannelContact == null) {
        if (other.salesChannelContact != null)
            return false;
    } else if (!salesChannelContact.equals(other.salesChannelContact))
        return false;
    if (salesChannelId != other.salesChannelId)
        return false;
    return true;
}
    @Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((companyName == null) ? 0 : companyName.hashCode());
    result = prime * result + ((country == null) ? 0 : country.hashCode());
    result = prime * result + (isActive ? 1231 : 1237);
    result = prime
            * result
            + ((salesChannelContact == null) ? 0 : salesChannelContact
                    .hashCode());
    result = prime * result + salesChannelId;
    return result;
}
}   // END class SalesChannel
like image 335
Amir Avatar asked Mar 10 '14 13:03

Amir


People also ask

How do I compare two object values 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)) .

Can assertEquals compare objects?

Yes, assertEquals() invokes the overridden equals() if the class has one. Show activity on this post. 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 .

Which method is used to compare two objects?

The equals() method of the Object class compare the equality of two objects. The two objects will be equal if they share the same memory address.

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

@Test
public void testMyClass(){

    MyClass objExpected = new MyClass();
    objExpected.setMyClasslId(79662);
    objExpected.setMyClassCompanyName("BMW");
    objExpected.setCountry("DE");
    objExpected.setActive(true);

    String jsonStringActual = "{\"myClassId\":79662,"
            + "\"myClasscompanyName\":\"BMW\","
            + "\"country\":\"DE\",\"isActive\":true,"
            + "\"MyClassContacts\":[]}";
    MyClass objcActual = gson.fromJson(jsonStringActual, MyClass.class);        
    System.out.println(objExpected.toString());
    System.out.println(objActual.toString());
    assertEquals(objExpected, objActual);           
}

Run this code and paste your input

like image 62
ruhungry Avatar answered Oct 24 '22 19:10

ruhungry


Your equals method is using a specific class and not Object.

You need to override the equals(Object obj) method with your own implementation.

If in that you first perform typechecking, and then call your implemented equals class you should have more luck.

assertEquals is calling the equals(Object obj) method of the super class (in your case Object) which by default just compares the reference of the object. As they are not the same object your result will fail.

So if you look at it as the interpreter sees it.

public class SalesChannel extends Object
{
}

public class Object
{
    public boolean equals(Object obj)
    {
        return this == obj;
    }
}

So to fix it

public class SalesChannel 
{
    @Override public boolean equals(Object obj)
    {
        if (obj != null && obj.isInstance(SalesChannel.class))
            return this.equals((SalesChannel) obj);
        return false;
    }
}
like image 25
sweetfa Avatar answered Oct 24 '22 17:10

sweetfa