Method SendMessage to ActiveMq Test

85 views
Skip to first unread message

Karol Pawłowski

unread,
Mar 6, 2012, 9:24:22 AM3/6/12
to NSubstitute
Hi. I have to create test by NSubstitute framework. This is the
method:

public interface IProducer
{
void Send(Message message);
}

sealed internal class Producer : IProducer
{
private Apache.NMS.IMessageProducer _producer;
public Producer(Apache.NMS.IMessageProducer producer)
{
this._producer = producer;
}
public void Send(Message message)
{
IMessage msg
=_producer.CreateXmlMessage(message);
_producer.Send(msg);
}
}


and this is my test:

[Test]
public void SendMessageTest()
{
// Objects
var producerProxy = Substitute.For<IMessageProducer>();
var messageProxy = Substitute.For<ITextMessage>();
var messageProx = Substitute.For<Message>();
Producer producer = new Producer(producerProxy);

// record
producer.Send(messageProx);

// verify
producerProxy.Received().CreateXmlMessage(messageProx); //
here is problem
producerProxy.Received().Send(messageProxy);
}

Can someone tell me why selected method is invoking like original
method, no like mock method. I think this method should't be executed.

Thanks for any response

Abi

unread,
Mar 6, 2012, 2:52:39 PM3/6/12
to nsubs...@googlegroups.com
You forgot to mock
CreateXmlMessage() and
also use the return value to mock Send
For unit tests you have to Mock behaviour of external classes, in this case IMessageProducer

Sent from my iPhone

> --
> You received this message because you are subscribed to the Google Groups "NSubstitute" group.
> To post to this group, send email to nsubs...@googlegroups.com.
> To unsubscribe from this group, send email to nsubstitute...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nsubstitute?hl=en.
>

David Tchepak

unread,
Mar 6, 2012, 6:22:46 PM3/6/12
to nsubs...@googlegroups.com
Hi Karol, 

I am not sure I understand your question. When you say it is "invoking like original method", do you mean the Producer.Send() method?

  public void Send(Message message)
  {
       IMessage msg =_producer.CreateXmlMessage(message);
       _producer.Send(msg);
  }

This method is on a real (not substituted) class, so this method will be invoked normally and call CreateXmlMessage(...) and Send(msg) on the _producer substitute you passed into the constructor.

In that case I would expect both the assertions in your test (under "//verify") to pass. 

If it helps, you could make the mocked call more explicit by writing the test like this: 

[Test]
public void SendProducedMessage() {
  //Arrange (set up all the substitutes)
  var messageProducer = Substitute.For<IMessageProducer>();
  var message = Substitute.For<Message>();
  var producedMessage = Substitute.For<ITextMessage>();
  messageProducer.CreateXmlMessage(message).Returns(producedMessage);
  
  //Act (call real method)
  var subject = new Producer(producerProxy);
  subject.Send(message);
  
  //Assert (real method called Send on the IMessageProducer substitute)
  messageProducer.Received().Send(producedMessage);
}

Hope this helps.

Regards,
David

Anthony Egerton

unread,
Mar 6, 2012, 6:34:56 PM3/6/12
to nsubs...@googlegroups.com
On your producerProxy you need to set the return value for
CreateXmlMessage to return your messageProxy.

e.g. producerProxy.CreateXmlMessage(messageProx).Returns(MessageProxy);


On Wed, Mar 7, 2012 at 1:24 AM, Karol Pawłowski
<pawlowski...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages