【发布时间】:2012-02-23 07:52:44
【问题描述】:
您好,我正在努力解决简单的问题。
总体思路:
class Foo(){
public boolean method1();
public String method2();
public String method3();
public String shortcut(){
return (method1() == true) ? method2() : method3();
}
}
我应该如何测试快捷方式?
我知道如何模拟对象和测试使用其他对象的方法。示例:
class Car{
public boolean start(){};
public boolean stop(){};
public boolean drive(int km){};
}
class CarAutoPilot(){
public boolean hasGotExternalDevicesAttached(){
//Hardware specific func and api calls
//check if gps is available
//check if speaker is on
//check if display is on
}
public boolean drive(Car car, int km){
//drive
boolean found = hasGotExternalDevicesAttached();
boolean start = c.start();
boolean drive = c.drive(km);
boolean stop = c.stop();
return (found && start && drive && stop) == true;
}
}
class CarAutoPilotTest(){
@Test
public void shouldDriveTenKm(){
Car carMock = EasyMock.Create(Car.class);
EasyMock.expect(carMock.start()).andReturns(true);
EasyMock.expect(carMock.drive()).andReturns(true);
EasyMock.expect(carMock.stop()).andReturns(true);
EasyMock.reply(carMock);
CarAutoPilot cap = new CarAutoPilot();
boolean result = cap.drive(cap,10);
Assert.assertTrue(result);
EasyMock.verify(carMock);
}
}
但是 hasGotExternalDevicesAttached() 方法呢?这只是示例而非真实场景。我应该如何测试驱动方法?我还应该模拟 hasGotExternalDevicesAttached 函数吗?
我可以模拟正在测试的课程吗?
【问题讨论】:
-
+1 表示“我应该如何测试驾驶方法?”有趣的东西。
标签: java testing junit mocking tdd