5

I want to test an ActiveMQ messaging (SpringBoot in-memory) system. My problem is that JUnit @Test does not allow parameters for methods, but @JmsListener needs a parameter. How can I test that case? I have also no clue how to do that with JUnit Parameterized.class? Is there a way to run the test with the SpringBoot @JmsListener? Can anyone help me?

Note: The mqSend.sendJson() sends the same jsonString as you can see in the codesnippet.

Thank's for advice.

@Autowired
private MqSend mqSend;

@MockBean
private JmsTemplate jmsTemplate;

private String jsonString = "{ \"Number\": \"123456\", \"eMail\": \"mail@dummy.de\", \"Action\": \"add\" }";
private String receivedMessage;

@Before
public void setup() throws IOException {
    this.mqSend.sendJson();
    log.info("Setup done");
}

@Test
@JmsListener(destination = "${jms.queue}")
private void receiveMessageFromMQ(String message) {
    this.receivedMessage = message;
    log.info("Received Message: " + message);
}

@Test
public void test_sending_and_receiving_messages() {
    Assert.assertTrue(this.receivedMessage.equals(this.jsonString));
}

}

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
SenNoRikyu
  • 63
  • 1
  • 6
  • Your class makes no sense at all. An `@Test` on an `@JmsListener` method simply doesn't make sense. You should be sending a message from your test and somehow verify if your actual `@JmsListener` method has run. – M. Deinum Jul 21 '20 at 13:24
  • Duplicated https://stackoverflow.com/questions/42803627/writing-tests-to-verify-received-msg-in-jms-listener-spring-boot – Naou Jul 21 '20 at 13:37
  • Hi, thx for your reply. I still know that this makes not really sense. This snippet is just to show what I want. Do you have an idea how to verify in a testclass that my JmsListener method has run? And if the JmsListener method does not give me a clue if I received the right message. How can I run this method and how can I get the result to make an assert with that? – SenNoRikyu Jul 21 '20 at 13:44

1 Answers1

-1

Here an example, you have to @Autowire your JmsTemplate then use the methods you need to test :

@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, WOLRD!");
    }
}
Jay
  • 1,539
  • 1
  • 16
  • 27
Naou
  • 726
  • 1
  • 10
  • 23