Announcement Announcement Module
Collapse
No announcement yet.
How to use EasyMock to mock a MessageCreator for testing JmsTemplate.send()? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use EasyMock to mock a MessageCreator for testing JmsTemplate.send()?

    I have a method which looks like this:

    Code:
        public void registerHost(final String hostManifestLocation)
        {
            // send an AdminMessage for registering a host
            jmsTemplate.send(this.destination, new MessageCreator()
            {
                public Message createMessage(final Session session)
                    throws JMSException
                {
                    try
                    {
                        // build an AdminMessage for registering a host
                        AdminMessage adminMessage = new AdminMessage();
                        adminMessage.setActionType(AdminActionType.REGISTER_HOST);
                        adminMessage.setPhysicalHost(buildPhysicalHost(hostManifestLocation));
    
                        // return an XML representation of the AdminMessage
                        return session.createTextMessage(adminMessageXmlConverter.objectToXml(adminMessage));
                    }
                    catch (Exception ex)
                    {
                        throw new RuntimeException("Unable to register the host specified by the manifest file \'"
                                                   + hostManifestLocation + "\'", ex);
                    }
                }
            });
        }
    Most of the code I'll want to mock is happening within the new MessageCreator's createMessage() method. It's not clear how I could mock that, since the MessageCreator is an object which gets created anew each time the method runs. I know I can mock the JmsTemplate, Destination, Session, and AdminMessageXmlConverter and set these into the corresponding properties of my class and the JmsTemplate, but how I would go about mocking the MessageCreator is a mystery. Or maybe I'm taking the wrong approach and using mocks is the wrong way to test this kind of code?

    If anyone can give me some ideas I'll certainly appreciate it. Thanks in advance!

    --James

  • #2
    As a follow up I will show how I'm currently testing the code using EasyMock:

    Code:
        @Test
        public void testRegisterHost()
        {
            // mock objects used within the method under test
            Destination mockDestination = EasyMock.createMock(Destination.class);
            JmsTemplate mockJmsTemplate = EasyMock.createMock(JmsTemplate.class);
            mockJmsTemplate.send(EasyMock.eq(mockDestination), EasyMock.isA(MessageCreator.class));
            EasyMock.replay(mockJmsTemplate);
    
            // instantiate the class, set its properties, and execute the method under test
            AdminCLI adminCLI = new AdminCLI();
            adminCLI.setDestination(mockDestination);
            adminCLI.setJmsTemplate(mockJmsTemplate);
            adminCLI.registerHost(null);
    
            // verify that the mocks behaved as expected
            EasyMock.verify(mockJmsTemplate);
        }
    However this only verifies that the JmsTemplate.send() method is being called as expected, whereas it's more interesting for me to determine that the expected code within the MessageCreator's createMessage() method is executing as well, and this is what I can't figure out how to test using mocks. I hope this makes my question more clear.

    --James

    Comment


    • #3
      Subclass JmsTemplaye

      I have subclassed the JMSTempleta instead of mocking it:

      Code:
      		final Session session = EasyMock.createMock(Session.class);
      		JmsTemplate jmsTemplate = new JmsTemplate()
      		{
      			public void send(Destination destination,
      					MessageCreator messageCreator) throws JmsException {
      				try {
      					messageCreator.createMessage(session);
      				} catch (JMSException e) {
      					throw new RuntimeException(e);
      				}
      			}
      		};
      		//give here the expectations for your session

      Comment

      Working...
      X