Menu

JMS. verifyNumberOfReceivedTopicMessages

2009-11-09
2013-04-15
  • Nobody/Anonymous

    Hi there,

    I have a set of JMS based JUnit test cases. Basically I send a message to a topic and check that the message is received. The first test case is working as expected:

    - send one message to a topic
    - this.verifyNumberOfReceivedTopicMessages(MOCK_TOPIC, 1);

    If a copy and paste the code for that test and create a new test case it doesn't work and the assertion for the this.verifyNumberOfReceivedTopicMessages(MOCK_TOPIC, 1) fails even when it is the same code!? as before.

    Do I have to reset something after the first test case??

    Thanks a lot in advance
    Diego

     
  • Alwin Ibba

    Alwin Ibba - 2009-11-09

    If you extend BasicJMSTestCaseAdapter or JMSTestCaseAdapter and override setUp or tearDown you have to call the corresponding super method. If you don't extend the adapters but work with JMSTestModule and JMSMockObjectFactory you have to recreate both on each test.

     
  • Nobody/Anonymous

    Hi

    Thanks a lot for your answer. I have a call to the corresponding super method but still the same problem I am missing something for sure but I am not able to find it :( does anyone have an idea?

    public class MyClassTest extends JMSTestCaseAdapter {

        public MyClassTest() {
        }

        @Before
        public void setUp() throws JMSException, Exception {
            super.setUp();

            this.ejbTestModule = this.createEJBTestModule();

            this.connectionFactory = this.getJMSMockObjectFactory().getMockConnectionFactory();
            this.connection = this.connectionFactory.createConnection();
            this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            this.mockTopic = this.getDestinationManager().createTopic(MOCK_TOPIC);

            ejbTestModule.bindToContext(GENERIC_CONNECTION_FACTORY, this.connectionFactory);
            ejbTestModule.bindToContext(MOCK_TOPIC, this.mockTopic);

            this.connection.start();

            MockMessageListener mockMessageListener = new MockMessageListener();
            this.ejbTestModule.deployMessageBean(GENERIC_CONNECTION_FACTORY, MOCK_TOPIC, connectionFactory, mockTopic, mockMessageListener);
        }

        @After
        public void tearDown() throws JMSException, Exception {
            super.tearDown();
            this.session.close();
            this.connection.close();
        }

        @Test
        public void testVerifyNumberOfReceivedTopicMessages_ResultOK() throws JAXBException {

            // Preconditions
            assertTrue(this.getCurrentMessageListFromTopic(MOCK_TOPIC).isEmpty());

            SimpleTopicPublisher topicPublisher = new SimpleTopicPublisher();
    topicPublisher.sendOneMessage();

            this.verifyNumberOfReceivedTopicMessages(MOCK_TOPIC, 1); // Works As expected
            this.verifyReceivedTopicMessageAcknowledged(MOCK_TOPIC, 0);
            this.verifyReceivedTopicMessageEquals(MOCK_TOPIC, 0, expectedResponseMessage);
        }

        @Test
        public void testVerifyNumberOfReceivedTopicMessages_ResultFailed() throws JAXBException {

            // Preconditions
            assertTrue(this.getCurrentMessageListFromTopic(MOCK_TOPIC).isEmpty());

            SimpleTopicPublisher topicPublisher = new SimpleTopicPublisher();
    topicPublisher.sendOneMessage();

            this.verifyNumberOfReceivedTopicMessages(MOCK_TOPIC, 1); // Fails!!
            this.verifyReceivedTopicMessageAcknowledged(MOCK_TOPIC, 0);
            this.verifyReceivedTopicMessageEquals(MOCK_TOPIC, 0, expectedResponseMessage);
        }

    Thanks a lot in advance
    Diego

     
  • Alwin Ibba

    Alwin Ibba - 2009-11-09

    The adapters extend TestCase and are for JUnit 3.8. For JUnit 4 please do not extends the adpaters but do

    ejbTestModule = new EJBTestModule(new EJBMockObjectFactory());
    jmsFactory = new JMSMockObjectFactory();
    jmsTestModule = new JMSTestModule(jmsFactory);

     
  • Nobody/Anonymous

    Thanks, but it didn't work.

    I have the feeling this problem could be related to the fact that I am using a GenericConnection instead of a pure TopicConnection.  What do you think?

    By the way I am using JUnit 4.5

    Thanks

     
  • Alwin Ibba

    Alwin Ibba - 2009-11-09

    I don't think so because the first test is running. Just refactor your test so that it does not extend the adapter and if it still doesn't work step through the code with the debugger.

     

Log in to post a comment.

MongoDB Logo MongoDB