ResultState
オブジェクトを作成するのに、getResultState()
を使用していることは残念ですが、それは本当にあなたのブロッカーです。
protected ResultState initResultState ()
{
this.resultState = new ResultState();
// Initialization Logic...
return this.resultState;
} // END initResultState
public ResultState getResultState ()
{
if (this.resultState === null)
{
return this.initResultState();
}
return this.resultState;
} // END getResultState()
この位置から、ゲッターメソッドをテストする方が簡単です。それでももちろん、保護initResultState()
であなたを残し
/**
* The test fixutre's initResultState() method merely returns a simple Stub of
* the ResultState object, which can be more easily interrogated.
*/
public ResultState initResultState ()
{
return new Stub_ResultState();
} // END initResultState
:尋問することができinitResultState()
のために知られてResultState
スタブを返し子孫クラス、すなわちを定義します。以下のリファクタリングを考えてみましょう:
protected ResultState initResultState (ResultState newResultState)
{
this.resultState = newResultState;
// Initialization Logic...
return this.resultState;
} // END initResultState
public ResultState getResultState ()
{
if (this.resultState === null)
{
return this.initResultState(new ResultState());
}
return this.resultState;
} // END getResultState
これは、あなたが例えば、操作し、その後尋問するために別のResultState
スタブオブジェクトを渡すことができるような子孫クラスでgetResultState()
メソッドをオーバーライドすることを許可:
/**
* The test fixture's getResultState() method always acts as a passthrough for
* initResultState(), which is protected, so that the output of the protected
* method can be interrogated.
*/
public ResultState getResultState ()
{
return this.initResultState(new Stub_ResultState());
} // END getResultState
この位置から、2つのテストフィクスチャが必要です。ゲッタの機能をテストするためにinitResultState()
の動作をオーバーライドします。 initResultState()
の動作をテストするためのgetResultState()
の動作を無効にする別のものです。両方ともResultState
クラスの子孫であるが、単体テストでの問い合わせのための親オブジェクトの内部値へのパブリックアクセスを提供するStub_ResultState
クラスのインスタンスを使用します。
私はそれが理にかなっていることを希望しますが、説明を求めることは自由です。
+1 - 私が言うつもりだった言葉 –
Jon、 良い質問 - 何が重要なのか。 これについて考えると、結果の最も重要なことは、それを使用するメソッドによって期待されるオブジェクトでなければならないということです。そしてこれは// LOGICのコードの// LOGICセクションで行われます。ここでLOGICは "this"のフィールドを操作します 私は以下のテストシナリオを実行しました: 1.このフィールドのセッターとゲッターを設定しました 2. set + ve(良い値)と-ve(悪い値) 3.それらを例外なく構築してください... これは簡単なテストですが、最初は良いです。ありがとう –