Thomas Jaeger
Thomas Jaeger

Reputation: 943

How do I stub-out RabbitTemplate when using Spring Framework?

I'm trying to creating some integration tests and I want to stub out the constructor injected RabbitTemplate. What would be the best way of doing this? Here is an example of the current application service constructor and the amqp variable is used further down to create the messages. I'd like to use different stubs of the RabbitTemplate in order to create message scenarios.

private AccountRepository accountRepository;
private UserRepository userRepository;
private RabbitTemplate amqp;

@Autowired
public IdentityApplicationServiceImpl(UserRepository userRepository, AccountRepository accountRepository, 
        RabbitTemplate aRabbitTemplate) {
    this.userRepository = userRepository;
    this.accountRepository = accountRepository;
    this.amqp = aRabbitTemplate;
}

So, for userRepository, accountRepository, and aRabbitTemplate I want to use specific stubs based on the scenarios. Any tips if RabbitTemplate could be stubbed out would be great.

Upvotes: 1

Views: 627

Answers (1)

ESala
ESala

Reputation: 7058

Yes, it can be stubbed.

Just use the interface, RabbitOperations or AmqpTemplate.

A good approach would be to mock it and stub the methods you intend to use.

Upvotes: 2

Related Questions