Creating multiple instances of a mock object

View: New views
3 Messages — Rating Filter:   Alert me  

Creating multiple instances of a mock object

by Vince O'Sullivan :: Rate this Message:

| View Threaded | Show Only this Message

I modified the Publisher/Subscriber example to reject duplicate subscriber instances.  It ran and passed the test that I had created. I then added the following test to show that three different subscribers would each receive the message once, when piblished:

        @Test
        public void testPublish3()
        {
                // Set up test.
                final Publisher publisher = new Publisher();
                final Subscriber subscriber1 = context.mock(Subscriber.class);
                final Subscriber subscriber2 = context.mock(Subscriber.class);
                final Subscriber subscriber3 = context.mock(Subscriber.class);
                publisher.add(subscriber1);
                publisher.add(subscriber2);
                publisher.add(subscriber3);
                final String message = "message";

                // Set up expectations.
                context.checking(new Expectations() {{
                        exactly(1).of(subscriber1).receive(message);
                        exactly(1).of(subscriber2).receive(message);
                        exactly(1).of(subscriber3).receive(message);
                }});

                // Execute action.
                publisher.publish(message);

                // Verify results.
                context.assertIsSatisfied();
        }

This compiles fine but the test crashed with the following error:

java.lang.IllegalArgumentException: a mock with name subscriber already exists
        at org.jmock.Mockery.mock(Mockery.java:128)
        at org.jmock.Mockery.mock(Mockery.java:120)
        at jmock.PublisherTest.testPublish3(PublisherTest.java:61)  (i.e. where subscriber2 is declared, above).

How should I have written this test?

Thanks,
Vince.

Re: Creating multiple instances of a mock object

by Steve Freeman-2 :: Rate this Message:

| View Threaded | Show Only this Message

JMock enforces that multiple mock instances of the same type should have different names. That's what the error message is saying. Normally the instance name is defaulted. You could try:

final Subscriber subscriber1 = context.mock(Subscriber.class, "first");
final Subscriber subscriber2 = context.mock(Subscriber.class, "second");
final Subscriber subscriber3 = context.mock(Subscriber.class, "third");

S.


On 14 Mar 2010, at 15:03, Vince O'Sullivan wrote:

> I modified the Publisher/Subscriber example to reject duplicate subscriber
> instances.  It ran and passed the test that I had created. I then added the
> following test to show that three different subscribers would each receive
> the message once, when piblished:
>
> @Test
> public void testPublish3()
> {
> // Set up test.
> final Publisher publisher = new Publisher();
> final Subscriber subscriber1 = context.mock(Subscriber.class);
> final Subscriber subscriber2 = context.mock(Subscriber.class);
> final Subscriber subscriber3 = context.mock(Subscriber.class);
> publisher.add(subscriber1);
> publisher.add(subscriber2);
> publisher.add(subscriber3);
> final String message = "message";
>
> // Set up expectations.
> context.checking(new Expectations() {{
> exactly(1).of(subscriber1).receive(message);
> exactly(1).of(subscriber2).receive(message);
> exactly(1).of(subscriber3).receive(message);
> }});
>
> // Execute action.
> publisher.publish(message);
>
> // Verify results.
> context.assertIsSatisfied();
> }
>
> This compiles fine but the test crashed with the following error:
>
> java.lang.IllegalArgumentException: a mock with name subscriber already
> exists
> at org.jmock.Mockery.mock(Mockery.java:128)
> at org.jmock.Mockery.mock(Mockery.java:120)
> at jmock.PublisherTest.testPublish3(PublisherTest.java:61)  (i.e. where
> subscriber2 is declared, above).
>
> How should I have written this test?
>
> Thanks,
> Vince.
>
> --
> View this message in context: http://old.nabble.com/Creating-multiple-instances-of-a-mock-object-tp27895401p27895401.html
> Sent from the jMock - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Steve Freeman

Winner of the Agile Alliance Gordon Pask award 2006
Book: http://www.growing-object-oriented-software.com

+44 (0) 797 179 4105
M3P Limited.  http://www.m3p.co.uk
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Creating multiple instances of a mock object

by Vince O'Sullivan :: Rate this Message:

| View Threaded | Show Only this Message

Sorted.  Thanks.  Vince.
(On chapter 6 of GOOS;)
---------------------------------------------------------------------------------------------------

JMock enforces that multiple mock instances of the same type should have different names. That's what the error message is saying. Normally the instance name is defaulted. You could try:

final Subscriber subscriber1 = context.mock(Subscriber.class, "first");
final Subscriber subscriber2 = context.mock(Subscriber.class, "second");
final Subscriber subscriber3 = context.mock(Subscriber.class, "third");

S.