assertEquals - It checks that objects are equal or not based on the overridden equals () method of this class. Thus, we can check the equality of objects depending on their state (compare the values of the variables of their instances).
assertEquals () The assertEquals () method compares two objects for equality using their equals () method.
@Test public void assertEquals_example() { Employee employeeNew = new Employee(); employee.setSalary(1000000.0); assertEquals("EMPLOYEE OBJECT", employee, employeeNew); }
If two objects are equal according to the implementation of their equals () method, the assertEquals () method will return normally. Otherwise, the assertEquals () method will throw an exception, and the test will stop there.
assertSame () and assertNotSame () The assertSame () and assertNotSame () methods check if two references point to the same object. It is not enough that the two specified objects are equal according to their equals () methods. It must be exactly the same object that it points to.
Here is a simple example:
@Test public void assertSame_assertNoSame_example() { assertSame(employeeService.getEmployeeFromId(1), employeeService.getEmployeeFromId(1)); assertNotSame(employee, employeeService.getEmployeeFromId(1));
source share