junit测试中的断言

时间:2016-05-16 11:50:13

标签: java junit

我有方法:

public void addDepartment(String[] list, Department department) {
    if(list[0]!=null && list[0]!=""){
    department.setDepName(list[0]);
    this.departmentDao.addOrEditDepartment(department);
    }
}

我为它做了一个测试用例..

public void testAddDepartment(){
    Department department = new Department();
    String[] list = new String[1];
    list[0] = "mmm";
    department.setDepId(120);
    department.setDepName(list[0]);
    service.addDepartment(list, department);
    assertEquals("mmm",service.getDepartment(120).getDepName());

    //assertNotNull(service.getDepartment(112).getDepName());

}

但是出现了assertequals中的错误,其中assertequal不是函数(string,void)..但我不知道如何测试它...任何帮助plzz?

1 个答案:

答案 0 :(得分:2)

方法assertEquals检查两个对象的身份。

在这里检查:

  • department
  • 调用service.addDepartment(list, department)
  • 的结果

但方法addDepartment返回void,因此无法在此方法中使用。

您可能希望执行以下操作:

@Test
public void testAddDepartment(){
    Department department = new Department();
    String[] list = new String[1];
    list[0] = "mmm";
    department.setDepId(120);
    department.setDepName(list[0]);
    service.addDepartment(list, department);

    // Probably you need to change this to what you like to test
    assertArrayEquals(list, service.getListNotShownHere());

}