groovy 如何在spock中为enum添加单元测试?

x33g5p2x  于 2022-11-28  发布在  其他
关注(0)|答案(1)|浏览(139)

我正在进行以下枚举类

public enum EmployeeType {

    PERMANENT("10"),
    TEMPORARY("20"),
    PART_TIME("30");

    private final String employeeTypeId;

    EmployeeType(final String employeeTypeId) {
        this.employeeTypeId = employeeTypeId;
    }

    public String getEmployeeTypeId() {
        return employeeTypeId;
    }
}

尝试使用以下规格

class EmployeeTypeSpec extends Specification {
    
    @Unroll
    def "validate emp type"(EmployeeType employeeType) {
        expect:
        // want to assert each employeeType name() and employeeTypeId here
        employeeType.name()
        employeeType.employeeTypeId

        where:
        employeeType << EmployeeType.values()
    }
}

在这里,我使用values()方法传递每个枚举值。但不确定如何为每个枚举Assert名称和employeeTypeId?

p5fdfcr1

p5fdfcr11#

这样的代码可能比employeeType << EmployeeType.values()更有意义:

def "Test enum values"() {
    expect:
    value.employeeTypeId == typeId

    where:
    // could have more columns here if the enum
    // had more properties...
    value                  | typeId
    EmployeeType.PERMANENT | '10'
    EmployeeType.TEMPORARY | '20'
    EmployeeType.PART_TIME | '30'
}

相关问题