(I've tried to simplify the class relationships
for the illustration.)
I need to understand how, from TestQueueConfiguration, to
inject a mock that returns a pre-hatched hash map with the values
I want and avoid actually calling into MyProperties to get them.
Test QueueConfiguration such that mocked myProperties get
called instead of actual MyProperties. How to inject this
mock deep into QueueConfiguration -> QueueObjectManager
-> QueueObject?
Many thanks for comments.
public class TestQueueConfiguration
{
private QueueConfiguration configuration; //
class under test
@Mock private MyProperties myProperties; //
class we mock to avoid database
public void setUp()
{
MockitoAnnotations.initMocks( this );
}
@Test
public void test()
{
Map< String, Attribute > attributes = new
HashMap< String, Attribute >(1);
attributes.put( "key", new Attribute( "value" ) );
when( myProperties.getAttributes() ).thenReturn(
attributes );
configuration = new QueueConfiguration();
Map< String, String > result =
configuration.getQueueProperties( 1 ); <----
MyProperties down under here
assertEquals( result.size(), 1 );
}
}
// - class under test
--------------------------------------------------------------
public class QueueConfiguration
{
public QueueConfiguration() { }
Map< String, String > getQueueProperties( int
queueId )
{
QueueObjectManager qom = new
QueueObjectManager( queueId, "foo.conf" );
QueueObjectManager.QueueObject qO =
qom.getQueueObjectByQueueId( queueId );
Map< String, String > result = new
HashMap< String, String >();
if( qO.queueId == 1 )
result.putAll( qO.readPropertiesFromFile() );
<---- MyProperties down under here
return result;
}
}
// - class I want to mock
----------------------------------------------------------
public class MyProperties
{
Map< String, Attribute > attributes = new
HashMap< String, Attribute >();
public MyProperties( Map< String, Attribute > map
)
{
attributes = map;
}
public Map< String, Attribute > getAttributes()
{
// pretend this has gone to the database for these
attributes...
return attributes;
}
}
// - class used by class under test inside which I want
MyProperties mocked --------
public class QueueObjectManager
{
private QueueObject queueObject;
public QueueObjectManager( int queueId, String filename
)
{
this.queueObject = new QueueObject( queueId,
filename );
}
public QueueObjectManager.QueueObject
getQueueObjectByQueueId( int queueId )
{
QueueObject queue = queueObject;
if( queue.queueId == queueId )
return queue;
throw new NoSuchElementException( "Queue identified
does not exist" );
}
// - subclass where MyProperties is actually
used ---------
class QueueObject
{
protected int queueId;
protected String filename;
public QueueObject( int queueId, String filename )
{
this.filename = filename;
this.queueId = queueId;
}
// - method where MyProperties is used
------------------
public Map< String, String >
readPropertiesFromFile()
{
Map< String, String > result = new
HashMap< String, String >();
try
{
MyProperties props = new
ObjectRequester().getSecureObject( filename );
Map< String, Attribute > attributeMap
= props.getAttributes();
for( Map.Entry< String, Attribute >
map : attributeMap.entrySet() )
{
String key = map.getKey();
String value = getValue( map.getValue()
);
result.put( key, value );
}
}
catch( Exception e )
{
;
}
return result;
}
private String getValue( Attribute attribute )
{
return attribute.getValue();
}
}
}