【发布时间】:2015-08-22 07:19:03
【问题描述】:
我有一个遗留代码,其中仅包含 8700 行,每种方法的平均长度约为 200-300 行。我想知道我如何能够覆盖大部分代码,甚至获得成功结果的输出。
例如:
public class Aviation implements FlightStruct, FlightSystem extends Vehicles {
...var decoration...
...override method definitions...
public controls aviation(Flight request, Flight destination, Flight target) {
FlightResponse flightResponse = new FlightResponse();
flightResponse.speed = target.speed;
angularVel.calculate(greatCircle.flightResponse.speed);
_readVelData();
if (vel.circular.velocity <= 10000) {
for(countryName : country) {
calculateArrivalTime();
_readFuel();
if(fuel.limit < pwi.percent*100) {
createShortPathLandingPoint(Path newDestination, Register checkPoint);
}
}
}
private eqiptment company(String name, Vendor vendor) {
....calculate equiptment stuff that calls bunch of private methods
}
...alot more method here
}
}
我想知道我应该如何对这类代码进行单元测试? 我试图存根数据,但内部方法太复杂而无法遍历。通常一个方法会调用多层子方法。 例如.... 燃料类调用 _readfuel 并在 _readfuel 中调用destinationCheckPoint、flightWayPoint、flightSpeedCalc ...然后在每个子方法中它调用自己的子方法。 (例如,flightWaypoint 调用 calculateWayPoint、angVelocity、speedLimit,......) 总的来说,我不认为通过存根数据是可行的,因为很难找到正确的数据。 然后我的第二个选择是使用 Mockito 来模拟方法/类。这听起来更合理,但我再次遇到有多个子方法是私有的......并且mockito不能模拟私有方法。然后我尝试使用 PowerMockito 来模拟它工作几千行的私有方法,并且有这样的事情:
while (!report.successful && !telCList.isEmpty()) {
...
report.wayPoint = update.assignWayPoint(wayPointList); //null point exception
...
更新为空并且声明了它上面几百行的地方
UpdateGateWay update = setDestinationWayPoint(distance, speed, altitude);
setDestinationWayPoint 是一个私有方法,我使用 powerMockito 来模拟行为,我还将值作为存根的一部分返回。但是当它在下面被调用时,它变为空。
在这一点上我几乎放弃了。我想知道是否有更合理的方法来进行这种类型的测试......
【问题讨论】:
标签: java unit-testing mocking