1
我有这个类:我该如何测试?
public class EmployeeHours {
public static final Integer STEP = 60;
//more fields
private Integer minutes;
public void increaseMinutes(){//increase minutes by STEP}
public void decreaseMinutes(){//decrease minutes by STEP, 0 whether negative}
}
我想测试一下:
@Test
public void shouldBeZeroSinceMinutesCantBeNegative() {
int initialMinutes = 10;
EmployeeHours employeeHours = new EmployeeHours(null, 1, initialMinutes);
employeeHours.decreaseMinutes();
assertThat(employeeHours.getMinutes(), is(0));
}
该测试合格,怎么一回事,因为我知道,STEP
大于10但如果我改变STEP
是5,例如,它不会通过。
我应该在测试中控制这种情况吗?我的意思是:
if (STEP > initialMinutes)
then expectSomething
else expectAnotherThing
这是一个很好的做法吗?我该如何改进我的测试?
+1我认为这是一种很好的方法。在创建测试时,我会说添加条件并不是一个好主意,否则最终会增加复杂性。我通常努力做简单的测试,没有条件,并且在这个问题中,我尝试使变量/测试方法名称表达我想表达的内容。 –