Junit
provides below methods to check whether two object references are point to same object or not.
static public void
assertSame(String message, Object expected, Object actual)
Asserts
that two objects refer to the same object. If the expected and actual are not
pointing to same object, then junit throws AssertionError with given message.
static public void
assertSame(Object expected, Object actual)
Asserts
that two objects refer to the same object. If the expected and actual are not
pointing to same object, then junit throws AssertionError without message.
static public void
assertNotSame(String message, Object unexpected, Object actual)
Asserts
that two objects are not referring to the same object. If the expected and
actual are pointing to same object, then junit throws AssertionError with given
message.
static public void
assertNotSame(Object unexpected, Object actual)
Asserts
that two objects are not referring to the same object. If the expected and
actual are pointing to same object, then junit throws AssertionError without
message.
Find
the below working application.
DemoTestApp.java
package com.sample.arithmetic; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; import org.junit.Test; public class DemoTestApp { private static class Employee { private int id; private String firstName; private String lastName; public Employee(int id, String firstName, String lastName) { this.id = id; this.firstName = firstName; this.lastName = lastName; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); result = prime * result + id; result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Employee other = (Employee) obj; if (firstName == null) { if (other.firstName != null) return false; } else if (!firstName.equals(other.firstName)) return false; if (id != other.id) return false; if (lastName == null) { if (other.lastName != null) return false; } else if (!lastName.equals(other.lastName)) return false; return true; } } @Test public void testAssertSameWithMessage() { Employee emp1 = new Employee(1, "Krishna", "Gurram"); Employee emp2 = emp1; assertSame("emp1 & emp2 are not pointing to same object", emp1, emp2); } @Test public void testAssertSameWithOutMessage() { Employee emp1 = new Employee(1, "Krishna", "Gurram"); Employee emp2 = emp1; assertSame(emp1, emp2); } @Test public void testAssertNotSameWithMessage() { Employee emp1 = new Employee(1, "Krishna", "Gurram"); Employee emp2 = new Employee(1, "Krishna", "Gurram"); assertNotSame("emp1 & emp2 are pointing to same object", emp1, emp2); } @Test public void testAssertNotSameWithOutMessage() { Employee emp1 = new Employee(1, "Krishna", "Gurram"); Employee emp2 = new Employee(1, "Krishna", "Gurram"); assertNotSame(emp1, emp2); } }
No comments:
Post a Comment