单元测试 JMS 负载

huangapple 未分类评论45阅读模式
英文:

Unit Testing JMS payload

问题

我有一些代码,看起来像下面这样:

class MyClass {
    private JmsTemplate messageSender;

    public void doStuff(Domain object domainObject){
       ... //other stuff
       updateClient(domainObject);
    }
    ...
    private void updateClient(DomainObject domainObject){
        final String payload = buildMessageForObject(domainObject);
        messageSender.send(new MessageCreator() {
            public Message createMessage(Session sn) throws JMSException {
               return sn.createTextMessage(payload);
            }
        });
    }
}

我的JMS模板由Spring框架自动装配。buildMessageForObject方法的逻辑实际上是内联的,但如果它不是内联的,它将是私有的。我想要做的是针对消息载荷字符串进行断言。 如果Spring JMSTemplate API中有类似jmsTemplate.sendTextMessage(String text)的东西,这样我就可以针对在测试中模拟的jmsTemplate进行验证,但是在我和消息之间似乎有太多层的间接性,我在JMSTemplate或MessageCreator的API中看不到任何明显有助于我重构它以使其可测试的内容。我正在使用JUnit和Mockito,但我不知道Mockito是否允许我指定所创建的MessageCreator的行为。

提前感谢您的任何帮助。

英文:

I have some code that looks like the following:

class MyClass {
    private JmsTemplate messageSender;

    public void doStuff(Domain object domainObject){
       ... //other stuff
       updateClient(domainObject);
    }
    ...
    private void updateClient(DomainObject domainObject){
        final String payload = buildMessageForObject(domainObject);
        messageSender.send(new MessageCreator() {
            public Message createMessage(Session sn) throws JMSException {
               return sn.createTextMessage(payload);
            }
        });
    }
}

My JMS template is autowired by the Spring framework. The logic for buildMessageForObject is actually inlined, but if it weren't it would be private. What I'd like to do is to make assertions against the message payload string. It would be nice if the Spring JMSTemplate API had something like jmsTemplate.sendTextMessage(String text) so that I could make verifications against the mocked jmsTemplate in my tests, but there seems to be too many layers of indirection between me and the message, and I can't see anything in the API for JMSTemplate or MessageCreator that obviously helps me restructure it to make it testable. I'm using JUnit and Mockito, but I don't know of any way that Mockito would allow me to specify the behaviour of the created MessageCreator.

Thanks in advance for any help.

huangapple
  • 本文由 发表于 2020年4月3日 21:03:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/61012548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定