Search code examples
rabbitmqspring-amqpspring-rabbit

Mock AMQPTemplate inside TestSuite


I am running JUnits as a TestSuite. One of the JUnit class is for for testing the code which publishes the messages to RabbitMQ.

I am supposed to mock the code to publish the messages to MQ. How can I do that?

Is it possible to simply change the RabbitTemplate configuration inside configuration xml itself?

Class with business logic:

public class SomeClassImpl implements SomeClass {


@Autowired
@Qualifier("amqpTemplate")
private AmqpTemplate amqpTemplate;

public void someMethod(){
    ...
    amqpTemplate.convertAndSend(exactTargetQueue, etMsg);
    ...
}

Solution

  • Yes

    <bean id="rabbitAdmin" class="org.mockito.Mockito" factory-method="mock">
        <constructor-arg value="org.springframework.amqp.rabbit.core.RabbitTemplate" type="java.lang.Class"/>
    </bean>