ここで私は行く:jUnit、Mockito。抽象クラス(templaeメソッド)でメソッドを保証する方法は、抽象的なフックメソッドを呼び出しますか?
abstract class IdentifiedEntity<E extends IdentifiedEntity> implements Cloneable {
...
public void updateWith(E that) {
if (this != that) {
if (that.isNew()) {
throw new IllegalArgumentException("Cannot update with a new entity");
}
if (this.isNew()) {
throw new IllegalStateException("Cannot update a new entity");
}
if (this.getId() != that.getId()) {
throw new IllegalArgumentException("IDs do not match");
}
doUpdateWith(that);
}
}
abstract void doUpdateWith(E that);
...
}
public final class User extends IdentifiedEntity<User> {
...
@Override
void doUpdateWith(User that) {
assert that != null;
this.name = that.name;
this.email = that.email;
System.arraycopy(that.password, 0, password, 0, password.length);
this.enabled = that.enabled;
this.caloriesPerDayLimit = that.caloriesPerDayLimit;
}
...
}
質問は(はい、確かに、私がチェックしSALL tpasse場合)、それはdefinely子孫にimplmented抽象doUpdateWith(...)
を呼び出すことを確認するために、どのようにすることができます私のユニットテストupdateWith(...)
のですか?
あなたよね!このようなテストは非常に特別であるしかし
@Test
public void test() throws Exception {
ConcreteEntity e = Mockito.spy(new ConcreteEntity());
e.updateWith(e);
Mockito.verify(e).doUpdateWith(e);
}
:
それが役に立ったら、私の答えを受け入れるよりも親切だっただろう;)。 – CoronA
@CaronAはい、そうです – danissimo