Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing tests to verify received msg in jms listener (Spring-Boot)

I want to write test for something like below;

  1. There is a listener called state-info-1 in src/main.

  2. It does some changes to any message it gets and publishes the new message on activemq topic state-info-2.

  3. I will build a dummy message and publish on to activemq topic state-info-1.

  4. Finally verify that, the received message on topic state-info-2 is like i expected.

My Listeners are like;

@JmsListener(destination = "state-info-1", containerFactory = "connFactory")
public void receiveMessage(Message payload) {
    // Do Stuff and Publish to state-info-2
}

Is it possible i can write test for this? Or i have to do it in some other way?

Also, i looked at this : https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java

But this is not what i am expecting.

Any help or push in the right direction will be enough.

Thank you for your time.

like image 271
Rajkishan Swami Avatar asked Mar 15 '17 07:03

Rajkishan Swami


People also ask

How do you check if JMS listener is running?

The status of the listeners is always available by running the list listeners command from the user interface or the Transaction Server console. If a listener becomes disconnected, the Transaction Server can attempt to re-connect to the queue multiple times before it fails with a connection error.

What is Spring boot test annotation?

The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication , for instance) and use that to start a Spring application context. You can run this test in your IDE or on the command line (by running ./mvnw test or ./gradlew test ), and it should pass.


1 Answers

@SpringBootApplication
public class So42803627Application {

    public static void main(String[] args) {
        SpringApplication.run(So42803627Application.class, args);
    }

    @Autowired
    private JmsTemplate jmsTemplate;

    @JmsListener(destination = "foo")
    public void handle(String in) {
        this.jmsTemplate.convertAndSend("bar", in.toUpperCase());
    }

}

and

@RunWith(SpringRunner.class)
@SpringBootTest
public class So42803627ApplicationTests {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Test
    public void test() {
        this.jmsTemplate.convertAndSend("foo", "Hello, world!");
        this.jmsTemplate.setReceiveTimeout(10_000);
        assertThat(this.jmsTemplate.receiveAndConvert("bar")).isEqualTo("HELLO, WORLD!");
    }

}
like image 87
Gary Russell Avatar answered Nov 03 '22 16:11

Gary Russell