0

たとえば、このような基本的な POJO があるとします。

public class Stuff {

   private OtherStuff otherStuff;

   ...

   public void RunOtherStuff() {
       otherStuff.run();
   }

}

RunOtherStuff が otherStuff.run を呼び出すことをどのように正確にテストしますか?

私は現在、基本テスト フレームワークとして TestNG を使用しており、Rails や Ruby で rspec などを使用する方法と同様に、これをテストできる Java フレームワークに対して完全にオープンです。

4

2 に答える 2

0

基本的にOtherStuffMockOtherStuffのサブクラスであるaを記述します。メソッドをオーバーライドrun()して、次のように言いますSystem.out.println('Run is called');

OtherStuff今、Stuffクラスでセッターを持って、あなたのモッカーを渡します。

編集:

アサートするには、runWasCalled(デフォルトはfalse)というブール変数を使用して、内部でtrueに設定することができます。MocOtherStuff.run()

于 2013-01-09T20:56:12.170 に答える
0

otherStuff電話をかける前に、電話をかけるためのセッターを作成しRunOtherStuff()ます。setOtherStuff(myFake)

public class Stuff {

   private OtherStuff otherStuff;

   public void setOtherStuff(OtherStuff otherStuff) {
       this.otherStuff = otherStuff;
   }

   ...

   public void RunOtherStuff() {
       otherStuff.run();
   }

}

次に、テストは次のように記述できます。

private Stuff stuff;
private boolean runWasCalled;

public void setUp() {
    stuff = new Stuff();
    stuff.setOtherStuff(new OtherStuff() {
        public void run() {
            runWasCalled = true;
        }
    });
}



public void testThatOtherStuffRunMethodIsCalled() {
    stuff.RunOtherStuff();

    assertTrue(runWasCalled);
}
于 2013-01-09T20:47:34.453 に答える