gpt4 book ai didi

java - Spock 认为测试类的内部枚举应该是 Spec 的属性

转载 作者:行者123 更新时间:2023-12-01 13:18:42 32 4
gpt4 key购买 nike

我有一个 Java 类,正在尝试使用 Spock 进行测试。 Java 类包含一个内部枚举:

import static java.util.Calendar.*;
import java.util.*;

public class FederalHolidays {

public enum Observance {
NEW_YEARS_DAY(JANUARY, 1),
BIRTHDAY_OF_MARTIN_LUTHER_KING_JR(JANUARY, MONDAY, 3),
WASHINGTONS_BIRTHDAY(FEBRUARY, MONDAY, 3),
MEMORIAL_DAY(MAY, MONDAY, -1),
INDEPENDENCE_DAY(JULY, 4),
LABOR_DAY(SEPTEMBER, MONDAY, 1),
COLUMBUS_DAY(OCTOBER, MONDAY, 2),
VETERANS_DAY(NOVEMBER, 11),
THANKSGIVING_DAY(NOVEMBER, THURSDAY, 4),
CHIRSTMAS_DAY(DECEMBER, 25);

private final int month;
private final int dayOfMonth;
private final int dayOfWeek;
private final int weekOfMonth;
private static final int NA = 0;

private Observance(int month, int dayOfMonth) {
this.month = month;
this.dayOfMonth = dayOfMonth;
this.dayOfWeek = NA;
this.weekOfMonth = NA;
}

private Observance(int month, int dayOfWeek, int weekOfMonth) {
this.month = month;
this.dayOfMonth = NA;
this.dayOfWeek = dayOfWeek;
this.weekOfMonth = weekOfMonth;
}

boolean isFixedDate() {
return dayOfMonth != NA;
}
}

public Date dateOf(Observance observance, int year) {
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("EST"), Locale.ENGLISH);
cal.set(YEAR, year);
cal.set(MONTH, observance.month);
cal.clear(HOUR);
if (observance.isFixedDate()) {
cal.set(DAY_OF_MONTH, observance.dayOfMonth);
} else {
setNthDayOfWeek(cal, observance.dayOfWeek, observance.weekOfMonth);
}
adjustForWeekendsIfNecessary(cal);
return cal.getTime();
}

private void setNthDayOfWeek(Calendar cal, int dayOfWeek, int n) {
int week = 0;
int lastDay = cal.getActualMaximum(DAY_OF_MONTH);
int startDay = n > 0 ? 1 : lastDay;
int endDay = n > 0 ? lastDay : 1;
int incrementValue = n > 0 ? 1 : -1;
for (int day = startDay; day != endDay; day += incrementValue) {
cal.set(DAY_OF_MONTH, day);
if (cal.get(DAY_OF_WEEK) == dayOfWeek) {
week += incrementValue;
if (week == n) {
return;
}
}
}
}

private void adjustForWeekendsIfNecessary(Calendar cal) {
int dayOfWeek = cal.get(DAY_OF_WEEK);
cal.add(DAY_OF_MONTH, dayOfWeek == SATURDAY ? -1 : dayOfWeek == SUNDAY ? 1 : 0);
}
}

我的 Spock 测试规范如下所示:

 class FederalHolidaysSpec extends Specification {

@Shared
def federalHolidays = new FederalHolidays()

def "holidays are correctly calculated"() {
expect:
federalHolidays.dateOf(observance, year).format('yyyy/MM/dd') == date
where:
observance | year | date
NEW_YEARS_DAY | 2011 | '2010/12/31'
BIRTHDAY_OF_MARTIN_LUTHER_KING_JR | 2011 | '2011/01/17'
WASHINGTONS_BIRTHDAY | 2011 | '2011/02/21'
MEMORIAL_DAY | 2011 | '2011/05/30'
INDEPENDENCE_DAY | 2011 | '2011/07/04'
LABOR_DAY | 2011 | '2011/09/05'
COLUMBUS_DAY | 2011 | '2011/10/10'
VETERANS_DAY | 2011 | '2011/11/11'
THANKSGIVING_DAY | 2011 | '2011/11/24'
CHIRSTMAS_DAY | 2011 | '2011/12/26'
}
}

当我运行 Spock 时,我收到 10 个测试错误,表的每一行都有一个。每个错误都是相同的:

groovy.lang.MissingPropertyException: No such property: Observance for class:   bdkosher.FederalHolidaysSpec
at bdkosher.FederalHolidaysSpec.$spock_initializeFields(FederalHolidaysSpec.groovy)

(我使用的是 Spock 0.7-groovy-2.0、groovy-all 2.2.2(非 indy)和 Java 1.7.0_45。)

为什么 Spock 在我的 FederalHolidaysSpec 类上寻找名为 Observance 的属性?我最初怀疑该问题与内部枚举/静态导入有关,尽管将我的 Spock 测试更改为使用完全限定的枚举值(例如 bdkosher.FederalHolidays.Observance.CHRISTMAS_DAY)并没有产生任何影响。

编辑:Java 文件实现已更正,因此它通过了测试;测试文件中的拼写错误已更正。

最佳答案

确保源代码和测试用例都有正确的包。如果 java 类和测试类都在同一个包中,下面的方法对我有用。

package bdkosher

import spock.lang.Shared
import spock.lang.Specification
//import bdkosher.FederalHolidays.Observance //Do not need
import static bdkosher.FederalHolidays.Observance.*

class FederalHolidaysSpec extends Specification { .. }

尽管 spock 版本相同,但我在 Grails 应用程序中没有使用默认包名称 com.example 进行了测试。

关于java - Spock 认为测试类的内部枚举应该是 Spec 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22232391/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com