查找两个对象是否匹配

Find if two objects match

我有一个方法可以匹配两个对象。如果第一个对象的每个属性都与第二个对象的属性匹配,则会发生匹配。我知道在 Groovy 中我可以遍历一个对象的字段来执行此操作。问题是我遇到了一个我无法弄清楚的异常。

在代码中:

方法:private match(Project projectToMatch, Project project)

项目 class:

class Project 
{
    final String name
    final Person techLead
}

我想做这样的事情:

private boolean match(Project projectManaged, Project project) {
    project.properties.each { prop, value ->
        if(value != projectManaged["$prop"] {
            return false
        }
    }
    return true
}

但我得到:

java.lang.AssertionError: Closure  com.gustavo.tribal.ProjectManagerImplTest$_testEditProjectShouldNotEditIfPriorityAlreadyExists_closure3@32da5a84 
should have failed with an exception of type com.gustavo.tribal.exceptions.WrongProjectException, 
instead got Exception java.lang.NullPointerException: Cannot invoke method call() on null object

两个参数都不为null,但techLead的值是为两者准备的。我确定 null 值和异常之间存在关系,但我没弄对。

编辑:

测试是:

void testEditProjectShouldNotEditIfPriorityAlreadyExists() {
    projectManagerImpl.addProject(project)
    projectManagerImpl.addProject(project2)
    def msg = shouldFail(WrongProjectException) {
        projectManagerImpl.editProject(project, project2b)
    }
    assertEquals "Can't edit project as the requested priority (" + project2b.priority + ") is already in use", msg
}

以及 editProject 的代码:

void editProject(Project project, Project editedProject) {
    for(Project projectManaged : allProjects) {
        if(match(projectManaged, project)) {
            if(projectManaged.priority != editedProject.priority && usedPriorities.get(editedProject.priority)) {
                throw new WrongProjectException("Can't edit project as the requested priority (" + editedProject.priority + ") is already in use")
            }
            removeProject(project)
            addProject(editedProject)
        }
    }
}

原来我的 match 方法中缺少括号。另外,我应该使用 projectManaged[prop]

而不是 projectManaged["$prop"]