我有一个遗留代码,其中包含的每个方法的平均长度大约为200-300行,只有8700多行。我想知道如何才能够覆盖大部分代码,甚至能够获得成功结果的输出。如何测试大型复杂代码?
例如:
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模拟行为,我也返回值作为存根的一部分。但是当它在下面被调用时它变为空。
我几乎放弃了这一点。我想知道是否有更合理的方式来做这种类型的测试....