public class DepartmentServiceImpl implements DepartmentService {
@Autowired
private DepartmentDao departmentDao;
//... other functions
@Override
public Department getDepartment(int depid) {
return departmentDao.getDepartment(depid);
}
}
我的测试单位是
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));
}
但它给了我
java.lang.AssertionError: expected:<com.dineshonjava.model.Department@f9b5552> but was:<com.dineshonjava.model.Department@4d4960c8>
任何帮助??
答案 0 :(得分:3)
您应该覆盖Department类中的equals方法:
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;
}
}
答案 1 :(得分:1)
你试图断言两个逻辑相同的java对象,一个是你创建的,另一个是服务返回的。
正如某人的评论中所说,你需要覆盖你的平等方法并提供逻辑,因为这两个对象是相同的。在你的情况下,我猜两个不同的Department对象是相同的,如果他们有相同的depId。这个逻辑应该在Department.java的equals方法中,以使assertEqual工作。
或者,如果您不想这样做,您可以检查特定的原始值而不是整个对象比较。
@Test
public void testGetDepartment(){
department.setDepId(111);
department.setDepName("merna");
assertEquals(department.getDepName(), service.getDepartment(111).getDepName());
}
但是假设您在测试用例中放置的值与DB(或从任何地方)返回的值相同。