测试从通用抽象 class 扩展的 class
Testing a class that extends from generic abstract class
嗯,这是我第一次做测试。我的代码是下一个:
public abstract class Value<T>
{
private T value;
public Value(T aValue) {
this.value = value;
}
public T value() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Value<?> that = (Value<?>) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
然后我创建一个 class 从上面扩展 class:
public class DateOfBirth extends Value<LocalDate>
{
private DateOfBirth(LocalDate aDate) {
super(aDate);
}
public static DateOfBirth of(LocalDate aDate) {
return new DateOfBirth(aDate);
}
}
终于我运行下文了,但是没通过
public class DateOfBirthUnitTest
{
@Test
public void birthDateShouldBeInstanceFromLocalDate() {
DateOfBirth theActual = DateOfBirth.of(LocalDate.of(1997, 12, 10));
assertNotNull(theActual.value()); //This test is failing
}
}
为什么“theActual.value()”调用返回 NULL 值?我该如何解决?
谢谢。
打字错误...??
改变
public Value(T aValue) {
this.value = value;
}
到
public Value(T value) {
this.value = value;
}
它应该可以工作!
嗯,这是我第一次做测试。我的代码是下一个:
public abstract class Value<T>
{
private T value;
public Value(T aValue) {
this.value = value;
}
public T value() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Value<?> that = (Value<?>) o;
return Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
然后我创建一个 class 从上面扩展 class:
public class DateOfBirth extends Value<LocalDate>
{
private DateOfBirth(LocalDate aDate) {
super(aDate);
}
public static DateOfBirth of(LocalDate aDate) {
return new DateOfBirth(aDate);
}
}
终于我运行下文了,但是没通过
public class DateOfBirthUnitTest
{
@Test
public void birthDateShouldBeInstanceFromLocalDate() {
DateOfBirth theActual = DateOfBirth.of(LocalDate.of(1997, 12, 10));
assertNotNull(theActual.value()); //This test is failing
}
}
为什么“theActual.value()”调用返回 NULL 值?我该如何解决?
谢谢。
打字错误...??
改变
public Value(T aValue) {
this.value = value;
}
到
public Value(T value) {
this.value = value;
}
它应该可以工作!