Tested object in an Expectation section

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

Tested object in an Expectation section

by Mac-39 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello there,
I wonder if there is easy way to change jmock lib to allow tested object be
in Expectation section. For example:

context.checking(new Expectations() {{
one (receiverMock1).recv("a"); inSequence(SenderSequence); //mock object
one (senderTest).ack("a"); inSequence(SenderSequence); //tested object
one (receiverMock1).recv("b"); inSequence(SenderSequence); //mock
object }});

I know that jmock is a library for unit testing but I would like to use it
in my project to check how objects are communicating in a sequence. I am
mocking one site and next I am testing sequence for first object. But there
is a problem when this mocked object is calling tested object method.

Sorry for my English. I am waiting for some ideas. Thanks.

Regards,
Mac




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

    http://xircles.codehaus.org/manage_email



Re: Tested object in an Expectation section

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm sorry, but that doesn't really make sense. There should be no way  
for receiver1 to receive an event before the triggering ack(). I'm  
sure there's another way, but we need to understand your code better.  
Can you give us more detail?

S.

On 19 Jun 2008, at 21:29, Mac wrote:

> Hello there,
> I wonder if there is easy way to change jmock lib to allow tested  
> object be
> in Expectation section. For example:
>
> context.checking(new Expectations() {{
> one (receiverMock1).recv("a"); inSequence(SenderSequence); //mock  
> object
> one (senderTest).ack("a"); inSequence(SenderSequence); //tested object
> one (receiverMock1).recv("b"); inSequence(SenderSequence); //mock
> object }});
>
> I know that jmock is a library for unit testing but I would like to  
> use it
> in my project to check how objects are communicating in a sequence.  
> I am
> mocking one site and next I am testing sequence for first object.  
> But there
> is a problem when this mocked object is calling tested object method.
>
> Sorry for my English. I am waiting for some ideas. Thanks.
>
> Regards,
> Mac



Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
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: Tested object in an Expectation section

by Mac-39 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>I'm sorry, but that doesn't really make sense. There should be no way  for
>receiver1 to receive an event before the triggering ack(). I'm  sure
>there's another way, but we need to understand your >code better.  Can you
>give us more detail?
>
>S.

So here is simple example:

I have two classes Sender and Receiver. They have references to each other
(Sender has reference to Receiver and vice versa).
And now Sender will "send" message to Receiver using Receiver's recv()
method. After that Receiver is calling ack() method on
Sender to acknowledge transmission. So now I want to prepare Expectations
section like this:

one (receiverMock1).recv("a"); inSequence(SenderSequence); //mock object
one (senderTest).ack("a"); inSequence(SenderSequence); //tested object

Here I am testing Sender class. I know I can create action and it should
look something like this:

one (receiverMock1).recv("a"); inSequence(SenderSequence);
will(callAckMethod(senderTest,"a"));

but next I am  not testing calling ack() method in the sequence.

Here is whole code:

Unit test:
///////////////////////
package case1.test;

import junit.framework.TestCase;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.Sequence;
import org.jmock.api.Action;
import org.jmock.lib.legacy.ClassImposteriser;

import case1.Receiver;
import case1.Sender;
import case1.action.CallBackAction;

public class SequenceAckTest extends TestCase {
 public static Action callAckMethod(Sender sender, String msg) {
     return new CallBackAction(sender,msg);
 }

  public void testSender() {
      // set up
   Mockery context = new Mockery() {{
        setImposteriser(ClassImposteriser.INSTANCE);
    }};

  //neighbors
  final Receiver receiverMock1 = context.mock(Receiver.class, "receiver1");

  // test object initialization
  Sender senderTest = new Sender(receiverMock1);

  final Sequence SenderSequence = context.sequence("SenderSequence");

  // expectations
  context.checking(new Expectations() {{
   one (receiverMock1).recv("a"); inSequence(SenderSequence);
//will(callAckMethod(senderTest,"a"));
   one (senderTest).ack("a"); inSequence(SenderSequence); //ERROR this will
not compile
      }});

      // execute - start testing
      senderTest.protocol();

      context.assertIsSatisfied();
  }
}
///////////////////////
Sender:
///////////////////////
package case1;

public class Sender{
 private Receiver receiver1;

 private void sendMessageA(){
  receiver1.recv("a");
 }

 public Sender(Receiver rec1){
  this.receiver1=rec1;
 }

 public void protocol(){
  //sending message
  sendMessageA();
 }

 public void ack(String msg){
  System.out.println("Ack received: "+msg);
 }
}
///////////////////////
Receiver:
///////////////////////
package case1;

public class Receiver {
private Sender sender;

public Receiver(Sender sender){
this.sender=sender;
}

public void recv(String msg){
System.out.println("Receiver: "+msg);
sender.ack(msg);
}
}
///////////////////////

Thanks for replying.

Regards,
Mac




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

    http://xircles.codehaus.org/manage_email



Re: Re: Tested object in an Expectation section

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not really sure what you're trying to test.

There's no point in testing that the receiver calls the sender's ack
method, because the receiver is a mock object and does whatever the
test tells it to do.

If you want to test that a *real* receiver calls ack on a sender, then
you'll need to use a mock sender when you test the implementation of
receiver.

--Nat


2008/6/21 Mac <maciek.kepinski@...>:

>>I'm sorry, but that doesn't really make sense. There should be no way  for
>>receiver1 to receive an event before the triggering ack(). I'm  sure
>>there's another way, but we need to understand your >code better.  Can you
>>give us more detail?
>>
>>S.
>
> So here is simple example:
>
> I have two classes Sender and Receiver. They have references to each other
> (Sender has reference to Receiver and vice versa).
> And now Sender will "send" message to Receiver using Receiver's recv()
> method. After that Receiver is calling ack() method on
> Sender to acknowledge transmission. So now I want to prepare Expectations
> section like this:
>
> one (receiverMock1).recv("a"); inSequence(SenderSequence); //mock object
> one (senderTest).ack("a"); inSequence(SenderSequence); //tested object
>
> Here I am testing Sender class. I know I can create action and it should
> look something like this:
>
> one (receiverMock1).recv("a"); inSequence(SenderSequence);
> will(callAckMethod(senderTest,"a"));
>
> but next I am  not testing calling ack() method in the sequence.
>
> Here is whole code:
>
> Unit test:
> ///////////////////////
> package case1.test;
>
> import junit.framework.TestCase;
>
> import org.jmock.Expectations;
> import org.jmock.Mockery;
> import org.jmock.Sequence;
> import org.jmock.api.Action;
> import org.jmock.lib.legacy.ClassImposteriser;
>
> import case1.Receiver;
> import case1.Sender;
> import case1.action.CallBackAction;
>
> public class SequenceAckTest extends TestCase {
>  public static Action callAckMethod(Sender sender, String msg) {
>     return new CallBackAction(sender,msg);
>  }
>
>  public void testSender() {
>      // set up
>   Mockery context = new Mockery() {{
>        setImposteriser(ClassImposteriser.INSTANCE);
>    }};
>
>  //neighbors
>  final Receiver receiverMock1 = context.mock(Receiver.class, "receiver1");
>
>  // test object initialization
>  Sender senderTest = new Sender(receiverMock1);
>
>  final Sequence SenderSequence = context.sequence("SenderSequence");
>
>  // expectations
>  context.checking(new Expectations() {{
>   one (receiverMock1).recv("a"); inSequence(SenderSequence);
> //will(callAckMethod(senderTest,"a"));
>   one (senderTest).ack("a"); inSequence(SenderSequence); //ERROR this will
> not compile
>      }});
>
>      // execute - start testing
>      senderTest.protocol();
>
>      context.assertIsSatisfied();
>  }
> }
> ///////////////////////
> Sender:
> ///////////////////////
> package case1;
>
> public class Sender{
>  private Receiver receiver1;
>
>  private void sendMessageA(){
>  receiver1.recv("a");
>  }
>
>  public Sender(Receiver rec1){
>  this.receiver1=rec1;
>  }
>
>  public void protocol(){
>  //sending message
>  sendMessageA();
>  }
>
>  public void ack(String msg){
>  System.out.println("Ack received: "+msg);
>  }
> }
> ///////////////////////
> Receiver:
> ///////////////////////
> package case1;
>
> public class Receiver {
> private Sender sender;
>
> public Receiver(Sender sender){
> this.sender=sender;
> }
>
> public void recv(String msg){
> System.out.println("Receiver: "+msg);
> sender.ack(msg);
> }
> }
> ///////////////////////
>
> Thanks for replying.
>
> Regards,
> Mac
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

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

    http://xircles.codehaus.org/manage_email



Re: Re: Tested object in an Expectation section

by Mac-39 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for replying.

I dont want to use jmock for unit testing. It will be something more -
testing accordance with documentation (UML sequence diagrams) in
communication between objects. I would like to test in isolation one object
for example Sender, his communication with Receiver.

And my main question is if it is possible to change jmock lib this way to
allow putting testing object in expectation section. This library has a lot
of functionality which I need and I dont want to write my own library from
scratch.


> I'm not really sure what you're trying to test.
>
> There's no point in testing that the receiver calls the sender's ack
> method, because the receiver is a mock object and does whatever the
> test tells it to do.
>
> If you want to test that a *real* receiver calls ack on a sender, then
> you'll need to use a mock sender when you test the implementation of
> receiver.
>
> --Nat
>
>
> 2008/6/21 Mac <maciek.kepinski@...>:
> >>I'm sorry, but that doesn't really make sense. There should be no way
> >>for
> >>receiver1 to receive an event before the triggering ack(). I'm sure
> >>there's another way, but we need to understand your >code better. Can
> >>you
> >>give us more detail?
> >>
> >>S.
> >
> > So here is simple example:
> >
> > I have two classes Sender and Receiver. They have references to each
> > other
> > (Sender has reference to Receiver and vice versa).
> > And now Sender will "send" message to Receiver using Receiver's recv()
> > method. After that Receiver is calling ack() method on
> > Sender to acknowledge transmission. So now I want to prepare
> > Expectations
> > section like this:
> >
> > one (receiverMock1).recv("a"); inSequence(SenderSequence); //mock object
> > one (senderTest).ack("a"); inSequence(SenderSequence); //tested object
> >
> > Here I am testing Sender class. I know I can create action and it should
> > look something like this:
> >
> > one (receiverMock1).recv("a"); inSequence(SenderSequence);
> > will(callAckMethod(senderTest,"a"));
> >
> > but next I am not testing calling ack() method in the sequence.
> >
> > Here is whole code:
> >
> > Unit test:
> > ///////////////////////
> > package case1.test;
> >
> > import junit.framework.TestCase;
> >
> > import org.jmock.Expectations;
> > import org.jmock.Mockery;
> > import org.jmock.Sequence;
> > import org.jmock.api.Action;
> > import org.jmock.lib.legacy.ClassImposteriser;
> >
> > import case1.Receiver;
> > import case1.Sender;
> > import case1.action.CallBackAction;
> >
> > public class SequenceAckTest extends TestCase {
> > public static Action callAckMethod(Sender sender, String msg) {
> > return new CallBackAction(sender,msg);
> > }
> >
> > public void testSender() {
> > // set up
> > Mockery context = new Mockery() {{
> > setImposteriser(ClassImposteriser.INSTANCE);
> > }};
> >
> > //neighbors
> > final Receiver receiverMock1 = context.mock(Receiver.class,
> > "receiver1");
> >
> > // test object initialization
> > Sender senderTest = new Sender(receiverMock1);
> >
> > final Sequence SenderSequence = context.sequence("SenderSequence");
> >
> > // expectations
> > context.checking(new Expectations() {{
> > one (receiverMock1).recv("a"); inSequence(SenderSequence);
> > //will(callAckMethod(senderTest,"a"));
> > one (senderTest).ack("a"); inSequence(SenderSequence); //ERROR this will
> > not compile
> > }});
> >
> > // execute - start testing
> > senderTest.protocol();
> >
> > context.assertIsSatisfied();
> > }
> > }
> > ///////////////////////
> > Sender:
> > ///////////////////////
> > package case1;
> >
> > public class Sender{
> > private Receiver receiver1;
> >
> > private void sendMessageA(){
> > receiver1.recv("a");
> > }
> >
> > public Sender(Receiver rec1){
> > this.receiver1=rec1;
> > }
> >
> > public void protocol(){
> > //sending message
> > sendMessageA();
> > }
> >
> > public void ack(String msg){
> > System.out.println("Ack received: "+msg);
> > }
> > }
> > ///////////////////////
> > Receiver:
> > ///////////////////////
> > package case1;
> >
> > public class Receiver {
> > private Sender sender;
> >
> > public Receiver(Sender sender){
> > this.sender=sender;
> > }
> >
> > public void recv(String msg){
> > System.out.println("Receiver: "+msg);
> > sender.ack(msg);
> > }
> > }
> > ///////////////////////
> >
> > Thanks for replying.
> >
> > Regards,
> > Mac
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> > http://xircles.codehaus.org/manage_email
> >
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>



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

    http://xircles.codehaus.org/manage_email



Re: Re: Re: Tested object in an Expectation section

by Nat Pryce :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/6/22 Mac <maciek.kepinski@...>:
> I dont want to use jmock for unit testing. It will be something more -
> testing accordance with documentation (UML sequence diagrams) in
> communication between objects. I would like to test in isolation one object
> for example Sender, his communication with Receiver.
>
> And my main question is if it is possible to change jmock lib this way to
> allow putting testing object in expectation section. This library has a lot
> of functionality which I need and I dont want to write my own library from
> scratch.

That's not what jMock is designed for.  But it provides components you
can use to write interceptors that sit between your real object and
test how they are communicating.  Look at the classes in the
org.jmock.lib package and subpackages.

--Nat

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

    http://xircles.codehaus.org/manage_email



Re: Re: Re: Tested object in an Expectation section

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In which case, it sounds like there's some confusion in the different  
levels of your app.

If you want to exercise the interplay between multiple objects as a  
cluster, then you might be better off hooking into a log or something  
and comparing traces in an integration test.

If your Sender and Receiver are wrappers around more complicated  
behaviour, you might be better extracting and mocking that, rather  
than Sender and Receiver directly.

If you just want to test how Sender talks to a Receiver, then probably  
Receiver should be an interface and the unit test should not exercise  
the ack(). And vice versa.

S.

On 22 Jun 2008, at 10:49, Mac wrote:

> Thanks for replying.
>
> I dont want to use jmock for unit testing. It will be something more -
> testing accordance with documentation (UML sequence diagrams) in
> communication between objects. I would like to test in isolation one  
> object
> for example Sender, his communication with Receiver.
>
> And my main question is if it is possible to change jmock lib this  
> way to
> allow putting testing object in expectation section. This library  
> has a lot
> of functionality which I need and I dont want to write my own  
> library from
> scratch.
>
>
>> I'm not really sure what you're trying to test.
>>
>> There's no point in testing that the receiver calls the sender's ack
>> method, because the receiver is a mock object and does whatever the
>> test tells it to do.
>>
>> If you want to test that a *real* receiver calls ack on a sender,  
>> then
>> you'll need to use a mock sender when you test the implementation of
>> receiver.
>>
>> --Nat
>>
>>
>> 2008/6/21 Mac <maciek.kepinski@...>:
>>>> I'm sorry, but that doesn't really make sense. There should be no  
>>>> way
>>>> for
>>>> receiver1 to receive an event before the triggering ack(). I'm sure
>>>> there's another way, but we need to understand your >code better.  
>>>> Can
>>>> you
>>>> give us more detail?
>>>>
>>>> S.
>>>
>>> So here is simple example:
>>>
>>> I have two classes Sender and Receiver. They have references to each
>>> other
>>> (Sender has reference to Receiver and vice versa).
>>> And now Sender will "send" message to Receiver using Receiver's  
>>> recv()
>>> method. After that Receiver is calling ack() method on
>>> Sender to acknowledge transmission. So now I want to prepare
>>> Expectations
>>> section like this:
>>>
>>> one (receiverMock1).recv("a"); inSequence(SenderSequence); //mock  
>>> object
>>> one (senderTest).ack("a"); inSequence(SenderSequence); //tested  
>>> object
>>>
>>> Here I am testing Sender class. I know I can create action and it  
>>> should
>>> look something like this:
>>>
>>> one (receiverMock1).recv("a"); inSequence(SenderSequence);
>>> will(callAckMethod(senderTest,"a"));
>>>
>>> but next I am not testing calling ack() method in the sequence.
>>>
>>> Here is whole code:
>>>
>>> Unit test:
>>> ///////////////////////
>>> package case1.test;
>>>
>>> import junit.framework.TestCase;
>>>
>>> import org.jmock.Expectations;
>>> import org.jmock.Mockery;
>>> import org.jmock.Sequence;
>>> import org.jmock.api.Action;
>>> import org.jmock.lib.legacy.ClassImposteriser;
>>>
>>> import case1.Receiver;
>>> import case1.Sender;
>>> import case1.action.CallBackAction;
>>>
>>> public class SequenceAckTest extends TestCase {
>>> public static Action callAckMethod(Sender sender, String msg) {
>>> return new CallBackAction(sender,msg);
>>> }
>>>
>>> public void testSender() {
>>> // set up
>>> Mockery context = new Mockery() {{
>>> setImposteriser(ClassImposteriser.INSTANCE);
>>> }};
>>>
>>> //neighbors
>>> final Receiver receiverMock1 = context.mock(Receiver.class,
>>> "receiver1");
>>>
>>> // test object initialization
>>> Sender senderTest = new Sender(receiverMock1);
>>>
>>> final Sequence SenderSequence = context.sequence("SenderSequence");
>>>
>>> // expectations
>>> context.checking(new Expectations() {{
>>> one (receiverMock1).recv("a"); inSequence(SenderSequence);
>>> //will(callAckMethod(senderTest,"a"));
>>> one (senderTest).ack("a"); inSequence(SenderSequence); //ERROR  
>>> this will
>>> not compile
>>> }});
>>>
>>> // execute - start testing
>>> senderTest.protocol();
>>>
>>> context.assertIsSatisfied();
>>> }
>>> }
>>> ///////////////////////
>>> Sender:
>>> ///////////////////////
>>> package case1;
>>>
>>> public class Sender{
>>> private Receiver receiver1;
>>>
>>> private void sendMessageA(){
>>> receiver1.recv("a");
>>> }
>>>
>>> public Sender(Receiver rec1){
>>> this.receiver1=rec1;
>>> }
>>>
>>> public void protocol(){
>>> //sending message
>>> sendMessageA();
>>> }
>>>
>>> public void ack(String msg){
>>> System.out.println("Ack received: "+msg);
>>> }
>>> }
>>> ///////////////////////
>>> Receiver:
>>> ///////////////////////
>>> package case1;
>>>
>>> public class Receiver {
>>> private Sender sender;
>>>
>>> public Receiver(Sender sender){
>>> this.sender=sender;
>>> }
>>>
>>> public void recv(String msg){
>>> System.out.println("Receiver: "+msg);
>>> sender.ack(msg);
>>> }
>>> }
>>> ///////////////////////
>>>
>>> Thanks for replying.
>>>
>>> Regards,
>>> Mac
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>> http://xircles.codehaus.org/manage_email
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>> http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
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


LightInTheBox - Buy quality products at wholesale price