It's unfortunate that you're using getResultState()
to create the ResultState
object, itself, which is really your blocker. Consider refactoring into the following interface:
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()
From this position, it's easier to test your getter method. Define a descendant class that returns a known ResultState
Stub for initResultState()
that can be interrogated, ie:
/**
* 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
That still leaves you with the protected initResultState()
, of course. Consider the following refactoring:
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
This permits you to override the getResultState()
method in a descendant class such that you can pass another ResultState
Stub object to be manipulated and interrogated afterward, for example:
/**
* 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
From this position, you need two test fixtures: one that overrides the behavior of initResultState()
to test the functionality of the getters; another that overrides the behavior of getResultState()
to test the behavior of initResultState()
. Both use an instance of the Stub_ResultState
class, which is necessarily a descendant of the ResultState
class but provides public access to the internal values of its parent object for interrogation in unit tests.
I hope that makes sense, but feel free to ask for clarifications.