使用 Spring Framework 时如何存根 RabbitTemplate?

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

我正在尝试创建一些集成测试,我想清除注入 RabbitTemplate 的构造函数。这样做的最佳方法是什么?这是当前应用程序服务构造函数的示例,amqp 变量进一步用于创建消息。我想使用 RabbitTemplate 的不同存根来创建消息场景。

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;
}

因此,对于 userRepository、accountRepository 和 aRabbitTemplate,我想根据场景使用特定的存根。如果 RabbitTemplate 可以被删除,任何提示都会很棒。

是的,可以存根

直接使用界面,RabbitOperationsAmqpTemplate

一个好的方法是模拟它并存根您打算使用的方法。