0

I have the following classes but my jUnit assertEquals() testing is not working. After days of searching and no answer I have decided to post here. Everything in the code seems right but it is still not working.

Contact class:

@Embeddable
public class Contact {

    @Column
    private String name;

    @Column
    private String address;

    @Column
    private String phone;

    public Contact(){
    }

    public Contact(String name, String address, String phone) {
        super();
        this.name = name;
        this.address = address;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String toString() {
        return "Contact [name=" + name + ", address=" + address + ", phone=" + phone + "]";
    }

    @Override
    public boolean equals(Object o){
        if(this == o){
            return true;
        }
        if(o == null){
            return false;
        }
        if(this.getClass() != o.getClass()){
            return false;
        }

        Contact contact = (Contact)o;

        if(name != contact.getName()){return false;}
        if(address != contact.getAddress()){return false;}
        if(phone != contact.getPhone()){return false;} 

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (address != null ? address.hashCode() : 0);
        result = 31 * result + (phone != null ? phone.hashCode() : 0);

        return result;          
    }       
}

Order class:

@Entity
public class Orders {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="weekday_recipient")),
        @AttributeOverride(name="address", column=@Column(name="weekday_address")),
        @AttributeOverride(name="phone", column=@Column(name="weekday_phone")),
    })
    private Contact weekdayContact;


    @Embedded
    @AttributeOverrides({
        @AttributeOverride(name="name", column=@Column(name="holiday_recipient")),
        @AttributeOverride(name="address", column=@Column(name="holiday_address")),
        @AttributeOverride(name="phone", column=@Column(name="holiday_phone"))
    })
    private Contact holidayContact;

    public Orders() {
        super();
    }

    public Long getId() {
        return id;
    }

    public Contact getWeekdayContact() {
        return weekdayContact;
    }
    public void setWeekdayContact(Contact weekdayContact) {
        this.weekdayContact = weekdayContact;
    }

    public Contact getHolidayContact() {
        return holidayContact;
    }
    public void setHolidayContact(Contact holidayContact) {
        this.holidayContact = holidayContact; 
    }

    public String toString() {
        return "orders [WEEKDAY_CONTACT=" + this.getWeekdayContact().toString() +  
         ", HOLIDAY_CONTACT=" + this.getHolidayContact().toString() + "]";
    }

    @Override
    public boolean equals(Object o){
        // self check
        if(this == o){
            return true;
        }
        // null check
        if(o == null){
            return false;
        }
        //type check and cast
        if(getClass() != o.getClass()){
            return false;
        }
        Orders orders = (Orders)o;
        // field comparison

        return Objects.equals(weekdayContact, orders.weekdayContact)
                &&
               Objects.equals(holidayContact, orders.holidayContact);


    }

}

Test Class

public class Chapter3Test {
    Contact weekdayContact = new Contact("Vienna", "Milkstreet 20", "+336598741");
    Contact holidayContact = new Contact("Cocos Island", "Gulay avenue 85", "+5589632147");
    Orders orders = new Orders();


    EntityManager em = EntitySessionManager.getEntityManager();


    @Before
    public void setUp(){
        orders.setWeekdayContact(weekdayContact);
        orders.setHolidayContact(holidayContact);
        em.getTransaction().begin();
    }

    @Test
    public void createOrders() {

        em.persist(orders);

        em.getTransaction().commit();
        em.close();
    }

    @Test
    public void retrieveOrders(){
        em = EntitySessionManager.getEntityManager();
        em.getTransaction().begin();

        Orders myOrders = em.find(Orders.class, 2L);

        assertEquals("They are not Equal", myOrders.getWeekdayContact(), orders.getWeekdayContact());
        assertEquals("They are not Equal", myOrders.getHolidayContact(), orders.getHolidayContact());

        em.getTransaction().commit();
        em.close();
    }

    @After
    public void closeResources(){
        //em.close();
    }

}

When i run my test, i get the following results::

java.lang.AssertionError: They are not Equal expected: chapter3.Contact<Contact [name=Vienna, address=Milkstreet 20, phone=+336598741]> but was: chapter3.Contact<Contact [name=Vienna, address=Milkstreet 20, phone=+336598741]>

You can see that what was expected and what was returned are the same.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Eddy Freeman
  • 3,207
  • 6
  • 35
  • 55
  • After the facts the question could be seen as DUP to the one I linked to in my answer; but I give you the credit of putting up a really nice, complete question with all the things required to solve the problem! – GhostCat Feb 23 '17 at 10:17

1 Answers1

3

Here:

private String name;

But then within your equals method:

if(name != contact.getName()){return false;}

You are comparing strings with == resp. != here.

Wrong (see here for details). Use name.equals(contact.getName()) instead!

It is not asssertEquals() that is wrong. That method just calls your implementation of equals(). So when the result is unexpected, then your implementation must be broken!

And hint: when comparing anything (esp. strings) do not trust that they look the same. The one and only thing that makes objects equal ... is the result of the equals() method. Anything else does not count!

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248