我有几个方法如何从接口实现接入领域的测试
public interface Turret {
void firePrimary();
void fireSecondary();
void reloadPrimary();
.......
}
我实现它已保护领域的接口。在执行上述方法时,我更改了受保护字段的值。
@Override
public void firePrimary() {
if(shootablePriAmmo > 0) {
shootablePriAmmo--; //Shootable ammo is a protected field inside the implementation
}else{
reloadPrimary();
}
}
现在我想测试火灾主要方法,并检查shootablePriAmmo
值是否被更改或没有,但因为这是一个接口的实现,我不能与接口的方法访问的字段,我不想写用于访问这些字段的接口中的方法,因为除了测试之外,这些方法不会在其他任何地方使用。如果我只在实现类上编写单元测试,那么我必须为这些字段创建公共getter,但是我没有看到它是正确的方式。我该怎么做才能克服这个问题?我违反任何面向对象的原则吗?
编辑 这里是实现
public BasicTurret(int maxPriAmmo, int maxSecAmmo, int priAmmoCap, int secAmmoCap) {
this.maxPriAmmo = maxPriAmmo;
this.maxSecAmmo = maxSecAmmo;
this.curPriAmmo = priAmmoCap;
this.curSecAmmo = secAmmoCap;
this.priAmmoCap = priAmmoCap;
this.secAmmoCap = secAmmoCap;
this.shootablePriAmmo = priAmmoCap;
this.shootableSecAmmo = secAmmoCap;
}
能否请您做一个标志一样吗? –
@suresh我不明白你的意思。 – Sneh