Turns out that FakeHapiConnectionHubFactory was not returning the same
instance both when defining the matcher in when() and in the code under
test. Consequently, the attach() method call under test did not match the
when() matcher because though its arguments may have matched, the object
instance was different, so their "this" pointers did not match.
To fix the problem, I changed FakeHapiConnectionHubFactory into a singleton
like ConnectionHub.getInstance():
public class FakeHapiConnectionHubFactory implements
HapiConnectionHubFactory
{
private static final ConnectionHub CONNECTION_HUB = mock(
ConnectionHub.class, withSettings().verboseLogging() );
@Override
public ConnectionHub newConnectionHub()
{
return CONNECTION_HUB;
}
}
Derek
On Fri, Aug 3, 2012 at 12:56 PM, Derek Mahar <derek.ma...@intelerad.com>wrote:
> How does one mock ConnectionHub.attach() using Mockito? Here is my
> unsuccessful attempt where the matcher fails to match:
>
> public TestHl7GatewayStatusUpdateSender() throws HL7Exception
> {
> mConnectionHubFactory = new FakeHapiConnectionHubFactory();
> Connection fakeConnection = mock( Connection.class );
> ConnectionHub connectionHub =
> mConnectionHubFactory.newConnectionHub();
> when( connectionHub.attach( anyString(),
> anyInt(),
> (Parser) any(),
> (MinLowerLayerProtocol)any(),
> anyBoolean() ) ).thenReturn(
> fakeConnection );
>
> }
>
> public class FakeHapiConnectionHubFactory implements
> HapiConnectionHubFactory
> {
> @Override
> public ConnectionHub newConnectionHub()
> {
> return mock( ConnectionHub.class, "fake connection hub" );
> }
> }
>
>
> Code under test:
>
> ...
>
> // Returns the same "fake connection hub" as in the constructor.
> ConnectionHub connectionHub =
> mConnectionHubFactory.newConnectionHub();
>
> // Returns null instead of mock connection.
> Connection connection =
> connectionHub.attach( "localhost",
> 5005,
> createNonValidatingPipingParser(),
> MinLowerLayerProtocol.class,
> false );
>
> // The initiator is used to transmit unsolicited messages to a
> server.
> Initiator initiator = connection.getInitiator();
> ...
>
> Any ideas?
>
> Derek
>
--
Derek Mahar
Senior Software Developer
Intelerad Medical Systems Incorporated
895 de la Gauchetiere W. Suite 400
Montreal, QC, Canada, H3B 4G1
Tel: 514.931.6222 x7754
Fax: 514.931.4653
derek.ma...@intelerad.com
www.intelerad.com
--
This email or any attachments may contain confidential or legally
privileged information intended for the sole use of the addressees. Any
use, redistribution, disclosure, or reproduction of this information,
except as intended, is prohibited. If you received this email in error,
please notify the sender and remove all copies of the message, including
any attachments.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Hl7api-devel mailing list
Hl7api-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hl7api-devel