So I am trying to get some integration testing of JMS processing, Spring (v4.1.6) based code.
It\'s a very standard Spring setup with @JmsListener annotate
I use spring profiles and have a different Processor in tests vs production code. In my test code, I write to a BlockingQueue after processing which can be waited on in the test
Eg:
@Configuration
public class MyConfiguration {
@Bean @Profile("!test")
public Processor productionProcessor() {
return new ProductionProcessor();
}
@Bean @Profile("test")
public Processor testProcessor() {
return new TestProcessor();
}
@Bean
public MyListener myListener(Processor processor) {
return new MyListener(processor);
}
}
public class MyListener {
private final Processor processor;
// constructor
@JmsListener(destination = "dest", containerFactory = "cf")
public void processMessage(TextMessage message) throws JMSException {
processor.process(message);
}
}
public class TestProcessor extends ProductionProcessor {
private final BlockingQueue queue = new LinkedBlockingQueue<>();
public void process(Textmessage message) {
super.process(message);
queue.add(message);
}
public BlockingQueue getQueue() { return queue; }
}
@SpringBootTest
@ActiveProfiles("test")
public class MyListenerTest {
@Autowired
private TestProcessor processor;
@Test
public void test() {
sendTestMessageOverMq();
TextMessage processedMessage = processor.getQueue().poll(10, TimeUnit.SECONDS);
assertAllOk(processedMessage);
}
}