请帮助我意识到我和我的代码有什么问题。简单的例子:
public class RollingStockCreation {
@Mocked Plant plant;
@Mocked RollingStock rollingStockMock;
@Mocked WrenchManagerInjection wm;
@Test
public void rollingStockCreationWithTwoMocks(){
new Expectations(){
{
wm.getCar();
result = new Delegate() {
Cargo delegate(){
return new Cargo(4,55.3);
}
};
}
};
Assert.assertEquals(wm.getCar().getChassisCount(),4);
}
}
public class Cargo extends RollingStock {
protected String cargoType;
public Cargo(int chassisCount, double maxWeight){
this.chassisCount = chassisCount;
this.maxWeight = maxWeight;
}
public String getCargoType() {
return cargoType;
}
public void setCargoType(String cargoType) {
this.cargoType = cargoType;
}
}
public abstract class Plant {
RollingStock rollingStock;
public RollingStock construct(RollingStock rollingStock){
this.rollingStock = rollingStock;
return rollingStock;
}
}
public class WrenchManagerInjection {
Plant plant;
RollingStock rollingStock;
@Inject
public WrenchManagerInjection(Plant plant, RollingStock rollingStock) {
this.plant = plant;
this.rollingStock = rollingStock;
}
public RollingStock getCar() {
return plant.construct(rollingStock);
}
public static RollingStock getCar(Plant plant, RollingStock rollingStock) {
return plant.construct(rollingStock);
}
}
我只需要返回模拟对象执行的真实结果。我试图使用委托、简单的结果 = 选项和返回(),但当我期望 4 和 55.3 时,它总是返回 0、0.00。我试图一个一个地模拟所有依赖项,模拟上层等等......我这样做只是为了学习,所以如果它在概念上错误,请告诉我。我当前的目标是 moke 所有依赖项并在执行期间返回 Cargo(4,55.3)。预先感谢。