Disable JMS consuming bean in unit-test

spring boot jms listener integration test
how to write junit test cases for jms
spring boot jms weblogic example
spring boot jms listener concurrency
spring boot activemq test
spring jms listener not receiving messages
spring boot jms topic subscriber example
embeddedactivemqbroker

I've build a Spring application that receives JMS messages (@JmsListener). During development I'd like to send some messages to the JMS-queue the listener listens to, so I wrote a unit-test that sends some messages (JmsTemplate). In this unit-test I use @SpringBootTest and @RunWith(SpringRunner.class) in order to load the application context (beans for datasources etc).

However, when the unit-test starts, it also loads the jms listener bean which directly starts consuming my new messages.

I would like to disable this jms listener bean in this test scenario so that messages are just added to the queue. Then later, I can start the main application and watch them being consumed.

How should I approach this?

I guess I could also have asked how I could disable a bean in general.

Thanks in advance.


You could use profiles to solve this problem.

To your listener, add the @Profile annotation:

@Profile("!test-without-jmslistener")
public class JmsListenerBean {
    ...
}

This tells Spring that it should only create an instance of this bean if the profile "test-without-jmslistener" is not active (the exclamation mark negates the condition).

On your unit test class, you add the following annotation:

@ActiveProfiles("test-without-jmslistener)
public class MyTest {
    ...
}

Now the Spring test runner will activate this profile before running your tests, and Spring won't load your bean.

How To Unit Test JMS Code - ActiveMQ, When unit testing code with JMS you'll typically want to avoid the overhead of running Disable broker persistence so that no queue purging is required before/after tests. new EmbeddedActiveMQBroker("bean:customize-activemq.​xml");. 5 Disable JMS consuming bean in unit-test May 24 '17 5 Tomcat: 1 war, deployed 2x, 2 configs Dec 8 '17 4 Flyway: how to prevent accidentally running migrations from developer on a central database?


Another solution to this problem: add @ComponentScan() to the test class to skip the loading of the specified beans.

@SpringBootTest
@RunWith(SpringRunner.class)
@ComponentScan(basePackages="com.pechen.demo", excludeFilters=@Filter(type=FilterType.ASSIGNABLE_TYPE, classes=JmsListener.class))
public class MyTest(){

}

Any more please refer to spring component scan include and exclude filters.

Mocking JMS infrastructure with MockRunner to favour testing, This allows us to test our JMS infrastructure without actually having This decoupling of the bean requirement (in the super context) from its actual Because unit and integration tests need to run fast, interaction with Remove a BOM Character From an Apache Camel Exchange Message (DSL Java). 6 Disable JMS consuming bean in unit-test May 24 '17. 5 Calculate number of days between two dates. Unexpected results May 24 '17. 3 Is a CPU core busy while reading


I think you can do it by this code:-

private void stopJMSListener() {
    if (customRegistry == null) {
        customRegistry = context.getBean(JmsListenerEndpointRegistry.class);
    }
    customRegistry.stop();
}

private void startJMSListener() {
    if (customRegistry == null) {
        customRegistry = context.getBean(JmsListenerEndpointRegistry.class);
    }
    customRegistry.start();
}

MockRunner with JMS Spring Unit Test, It is easy to test without JMS infrastructure such as Active MQ, OpenJMS http://​www.springframework.org/schema/beans/spring-beans-2.0.xsd. So when unit testing JMS code we recommend the following. Use an embedded broker to avoid a separate broker process being required. Disable broker persistence so that no queue purging is required before/after tests. It's often simpler and faster to just use Java code to create the broker via an XML configuration file using Spring etc.


Instead of using profiles you can also achieve this with a property:

@ConditionalOnProperty(name = "jms.enabled", matchIfMissing = true)
public class JmsListenerBean {
    ...
}

The matchIfMissing attribute tells Spring to set this property to true by default. In your test class you can now disable the JmsListenerBean:

@TestPropertySource(properties = "jms.enabled=false")
public class MyTest {
    ...
}

Getting Started with Spring JMS, A quick introduction to Spring JMS and its basic concepts. Get Started with Java · Java Concurrency Tutorials · Java String Tutorials · JUnit Tutorials · Java Algorithms System.out.println( "Message has been consumed : " + msg); bean id = "messageListener" class = "com.baeldung.spring.jms. SpringBoot-JMS with ActiveMQ. It's a simple Spring-Boot Microservice for Consuming messages from ActiveMQ Queues and Publish it to ActiveMQ Topics. I'm using MarshallingMessageConverter with Jaxb2Marshaller for both consuming and publishing XML message. I have also created Unit test and EmbeddedActiveMQ integration test. Run ActiveMQ.


Spring - Using @JmsListener to listen JMS messages, We need to use @JmsListener annotation on our listener methods. return connectionFactory; } @Bean public JmsListenerContainerFactory Kafka - Understanding Consumer Group with examples · Spring Boot - Unit Testing Web Spring Boot - Application Unit Testing with @SpringBootTest · Installing  So when unit testing JMS code we recommend the following. Use an embedded broker to avoid a separate broker process being required. Disable broker persistence so that no queue purging is required before/after tests. It’s often simpler and faster to just use Java code to create the broker via an XML configuration file using Spring etc.


Spring Boot Testing best practices, General recommendations to keep in mind while writing spring boot tests Often times, it is sufficient to use jUnit without loading any additional RunWith; import org.springframework.beans.factory.annotation. It is a good practice to mock the beans that are involved in database interactions, and turn off spring boot test  Note: If your application contains one or more message-driven beans, you may want to place the annotation on one of the message-driven beans. If you place the annotation on a sending component such as an application client, you need to specify the mappedName element to look up the topic, instead of using the destinationLookup property of the activation configuration specification.


Spring AMQP, In all of these cases, you can see similarities to the JMS support in the Spring Framework. The spring-rabbit-junit module is now a compile dependency in the implementation that is available only in the unit test code of the framework. Starting with version 1.7.1, Spring AMQP disables amqp-client automatic recovery  A message driven bean is a type of enterprise bean, which is invoked by EJB container when it receives a message from queue or topic. Message driven bean is a stateless bean and is used to do task asynchronously. To demonstrate use of message driven bean, we will make use of EJB-persistence chapter and we need to do the following tasks −