Playframework play 2.0-测试中未调用域对象上的setter
在游戏1.2.4中,当您使用UnitTest扩展您的测试类时,在测试期间调用域对象的setter,在游戏2.0中不会调用它们(参见下面的示例)。 我做错了什么 例如:Playframework play 2.0-测试中未调用域对象上的setter,playframework,playframework-2.0,Playframework,Playframework 2.0,在游戏1.2.4中,当您使用UnitTest扩展您的测试类时,在测试期间调用域对象的setter,在游戏2.0中不会调用它们(参见下面的示例)。 我做错了什么 例如: @Embeddable public class Amount { public static Amount zero() { Amount amount = new Amount(); //setValue should be called here by play framework
@Embeddable
public class Amount {
public static Amount zero() {
Amount amount = new Amount();
//setValue should be called here by play framework
amount.value = BigDecimal.ZERO;
return amount;
}
public static Amount of(double amount) {
Amount result = new Amount();
//setValue should be called here by play framework
result.value = BigDecimal.valueOf(amount);
return result;
}
public BigDecimal value;
public void setValue(BigDecimal value) {
this.value = rounded(value);
}
public Amount add(Amount amount) {
Amount result = new Amount();
result.value = rounded(this.value.add(amount.value));
return result;
}
private BigDecimal rounded(BigDecimal aNumber){
return aNumber.setScale(2, RoundingMode.HALF_EVEN);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Amount other = (Amount) obj;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
@Override
public String toString() {
return "Amount [value=" + value + "]";
}
}
1.2.4中的测试成功=>setValue在Amount上被调用
public class AmountTest extends UnitTest {
@Test
public void shouldBeAbleToRoundedHalfEven(){
Assertions.assertThat(Amount.of(1.055)).isEqualTo(Amount.of(1.06));
Assertions.assertThat(Amount.of(1.025)).isEqualTo(Amount.of(1.02));
Assertions.assertThat(Amount.of(1.016)).isEqualTo(Amount.of(1.02));
Assertions.assertThat(Amount.of(1.011)).isEqualTo(Amount.of(1.01));
Assertions.assertThat(Amount.of(1.010)).isEqualTo(Amount.of(1.01));
}
}
由于未调用setValue,2.0中的测试失败
public class AmountTest {
@Test
public void shouldBeAbleToRoundedHalfEven(){
running(fakeApplication(), new Runnable() {
public void run() {
assertThat(Amount.of(1.055)).isEqualTo(Amount.of(1.06));
assertThat(Amount.of(1.025)).isEqualTo(Amount.of(1.02));
assertThat(Amount.of(1.016)).isEqualTo(Amount.of(1.02));
assertThat(Amount.of(1.011)).isEqualTo(Amount.of(1.01));
assertThat(Amount.of(1.010)).isEqualTo(Amount.of(1.01));
}
});
}
}
Play 2没有Play 1的字节码增强功能,也不会像这样自动调用setter。如果您不想编写getter和setter,可以使用ProjectLombok(不过您需要在子项目中进行设置。请在PlayGoogleGroups论坛上搜索有关设置lombok的信息)。这将使您不必实现setX方法,但您仍然需要调用它,而不是直接访问属性