0

guys i have this service

public class DepartmentServiceImpl implements DepartmentService { @Autowired private DepartmentDao departmentDao; //... other functions @Override public Department getDepartment(int depid) { return departmentDao.getDepartment(depid); } } 

and my test unit is

Department department = new Department(); @Autowired private DepartmentDao myDao; @Autowired private DepartmentService service; @Test public void testGetDepartment(){ department.setDepId(111); department.setDepName("merna"); assertEquals(department, service.getDepartment(111)); } 

but it gives me

java.lang.AssertionError: expected:<com.dineshonjava.model.Department@f9b5552> but was:<com.dineshonjava.model.Department@4d4960c8> 

any help ??

2
  • 3
    Does your Department class override equals() ? Commented May 16, 2016 at 10:20
  • It isn't because they have the same ID that the objects are equals. Override equals and don't forget to override hashcode too respecting both contracts. Commented May 16, 2016 at 10:21

2 Answers 2

3

You should override the equals method in your Department class:

public boolean equals(Object object) { if(object == null) { return false; } if(this == object) { return true; } Department otherDepartment = (Department) object; if(this.getDepId() == otherDepartment.getDepId()) { return true; } else { return false; } } 
Sign up to request clarification or add additional context in comments.

Comments

1

you are trying to assert two different java object which are logically same, One which you created and other is returned by the service.

As told in comments by someone that you need to override your equal method and provide the logic as on what basis those two object are equal. in you case I guess two different Department object are equal if they have same depId. This logic should be there in equals method of Department.java for assertEqual to work.

Alternatively if you don't want to do that, you can check for particular primitive value rather than the whole object comparison.

@Test public void testGetDepartment(){ department.setDepId(111); department.setDepName("merna"); assertEquals(department.getDepName(), service.getDepartment(111).getDepName()); } 

But this is assuming that value you are putting in test case is same as it's being returned by DB (or from wherever).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.