Announcement Announcement Module
No announcement yet.
How to use EasyMock to mock a MessageCreator for testing JmsTemplate.send()? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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:

        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
                        // build an AdminMessage for registering a host
                        AdminMessage adminMessage = new AdminMessage();
                        // 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!


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

        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));
            // instantiate the class, set its properties, and execute the method under test
            AdminCLI adminCLI = new AdminCLI();
            // verify that the mocks behaved as expected
    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.



    • #3
      Subclass JmsTemplaye

      I have subclassed the JMSTempleta instead of mocking it:

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