简体   繁体   中英

Testing a JMS MessageListener

I'm not sure how to test a message listener. One thing is the error I receive. The other is I don't even know if I'm on the right track.

class TestListenerTest extends Specification {


    def "TestListener should receive a message"() {
        given:
        Message message = Mock ( Message )
        TestListener listener = new TestListener()

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory()
        JmsTemplate jmsTemplate = new JmsTemplate( connectionFactory )

        when:
        jmsTemplate.convertAndSend( "TEST_QUEUE_IN", message )

        then:
        1 * listener.onMessage( message )
    }
}

I get the following error:

AMQ229031: Unable to validate user from /127.0.0.1:53051. Username: null; SSL certificate subject DN: unavailable;

When I add a JMSContext with connectionFactory.createContext( "userName", "password") , the error is still there, just with userName instead of null . However, I'm not even sure if the way I test it is correct (barring the error).

I cannot say anything about your ActiveMQ problem, because I never worked with that product before, and you did not provide an MCVE (eg on GitHub) to reproduce it.

What I can say is that your Spock test can never work like this:

given:
TestListener listener = new TestListener()

// (...)

then:
1 * listener.onMessage( message )

You can only verify interactions on mocks or spies, but you instantiate listener like a normal object. So if you wish to check that the listener is actually being called, you need to mock it or spy on an instance. Furthermore, to me it does not look as if the listener has been wired into the system. So how does it know what to listen to? Maybe you have hidden the wiring right inside of class TestListener (which sounds a bit weird), but I have no way of knowing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM