如何模拟 EntityBus.rxSend() [英] How to mock EntityBus.rxSend()

查看:36
本文介绍了如何模拟 EntityBus.rxSend()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

io.vertx.reactivex.core.eventbus.EventBus.rxSend() 方法具有以下签名:

public <T> Single<Message<T>> rxSend(String address,
                                     Object message,
                                     DeliveryOptions options)

模拟它以返回包含真实对象的 Single 的正确方法是什么?问题是 Message 类除了接受另一个 Message 对象的构造函数之外没有其他构造函数.因此将编译以下内容:

What is the correct way to mock this so that it returns a Single containing a real object? The issue is that the Message class has no constructor apart from one which which takes another Message object. So the following will compile:

Mockito.when(eventBus.rxSend(Mockito.isA(String.class), 
   Mockito.isA(JsonObject.class), 
   Mockito.isA(DeliveryOptions.class))).thenReturn(Single.just(new Message<Object>(null)));

但当然 Single.just(new Message(null)) 不包含一个真实的对象,然后可以传递该对象以测试 Verticle 中的下一个处理程序.

but of course Single.just(new Message<Object>(null))does not contain a real object which can then be passed on to test the next handler in the verticle.

谢谢

推荐答案

就像我在评论中提到的那样,我无法回答您的直接问题,但我想推荐一种不同的方法来获得您正在寻找的结果.

like i mentioned in my comment, i don't have an answer to your immediate question, but i'd instead like to recommend a different approach to getting the results you're looking for.

出于各种原因,通常不鼓励模拟您不拥有的类型.最能引起我共鸣的两个(因为我已成为受害者)是:

mocking types that you don't own is generally discouraged for a variety of reasons. the two that resonate most with me (as i've fallen victim) are:

  • 如果模拟依赖项的实际实现发生变化,模拟的行为将不会自动显示任何具有前瞻性的更改.
  • 测试引入的模拟越多,测试带来的认知负担就越大.并且有些测试需要进行大量模拟才能工作.

有很多关于这个话题的文章,有更详细的观点和意见.如果您有兴趣,请参阅 Mockito wiki,或者只是谷歌一下.

there are lots of articles on the topic with more detailed viewpoints and opinions. if you're interested, refer to the Mockito wiki, or just Google around.

考虑到所有这些,与其模拟 EventBus,为什么不使用实际实例并接收由框架组成的真实回复 Messages?当然,严格来说这更像是集成测试而不是单元测试,但更接近于您想要的测试类型.

given all that, rather than mocking EventBus, why not use an actual instance and receive real reply Messages composed by the framework? sure, strictly speaking this becomes more of an integration test than a unit test, but is closer to the type of testing you want.

这是我在现有项目中编写的测试的示例片段,并添加了一些注释.(代码引用了一些带有 -"Ext" 后缀的非标准类型,但它们对于该方法并不突出).

here's an example snippet from a test i wrote in an existing project with some added comments. (the code refers to some non-standard types with an -"Ext" suffix, but they aren't salient to the approach).

private EventBus eventBus;

@Before
public setUp(@NotNull TestContext context) {
    eventBus = Vertx.vertx().eventBus()
}

@Test
public void ping_pong_reply_test(@NotNull TestContext context) {
    final Async async = context.async();

    // the following is a MessageConsumer registered 
    // with the EventBus for this specific test. 
    // the reference is retained so that it can be 
    // "unregistered()" upon completion of this test
    // so as not to affect other tests. 

    final MessageConsumer<JsonObject> consumer = eventBus.consumer(Ping.class.getName(), message -> {
        // here is where you would otherwise place
        // your mock Message generation.

        MessageExt.replyAsJsonObject(message, new Pong());
    });

    final Ping message = new Ping();
    final DeliveryOptions options = null;

    // the following uses an un-mocked EventBus to 
    // send an event and receive a real Message reply.
    // created by the consumer above.

    EventBusExt.rxSendJsonObject(eventBus, message, options).subscribe(
            result -> 
                // result.body() is JSON that conforms to 
                // the Pong type

                consumer.unregister();

                async.complete();
            },
            error -> {
                context.fail(error);
            }
    );
}

我希望这至少能激发一些关于您的问题的新想法.

i hope this at least inspires some new thinking around your problem.

这篇关于如何模拟 EntityBus.rxSend()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆