play 2.0 - 在测试中未调用的域对象的setter

时间:2012-03-17 12:25:05

标签: playframework playframework-2.0

在游戏1.2.4中,当您使用UnitTest扩展测试类时,在测试期间调用的域对象的setter,在play 2.0中它们不被调用(参见下面的示例)。 我做错了什么?

示例:

@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中的测试成功=>在Amount

上调用setValue
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));
    }


}

2.0中的测试失败,因为没有调用setValue

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));
               }
            });
    }

}

1 个答案:

答案 0 :(得分:0)

Play 2没有Play 1所做的字节码增强功能,也没有像这样自动调用setter。如果您不想编写getter和setter,可以使用项目lombok(您需要在子项目中进行设置。请在Play Google Groups论坛中搜索有关设置Lombok的信息)。这将使您免于必须实现setX方法,但您仍然必须调用它而不是直接访问该属性。