こんにちは私は簡単な問題で苦労しています。JUnit同じオブジェクト内の他のメソッドを使用するテストメソッド
一般的な考え方:
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「どのようにドライブメソッドをテストすればよいですか? Punnyもの。 –