JMockit - 相同类型的两个模拟实例

发布于 2024-11-02 14:51:24 字数 2021 浏览 1 评论 0 原文

我正在使用 JMockit 框架,并且正在尝试测试我的简单 EventBus 实现,该实现允许为 Event 类型注册 EventHandlers 。当事件在事件总线上触发时,所有注册的处理程序都会收到通知。事件可以被事件处理程序使用,这将导致后续处理程序不会收到该事件的通知。

我的测试方法如下所示:

// The parameter secondHandler should be mocked automatically by passing it
// as an argument to the test method
@Test
public void testConsumeEvent(final EventHandler<TestEvent> secondHandler)
{
    // create the event which will be fired and which the handlers are
    // listening to
    final TestEvent event = new TestEvent();

    // this handler will be called once and will consume the event
    final EventHandler<TestEvent> firstHandler = 
        new MockUp<EventHandler<TestEvent>>()
        {
            @Mock(invocations = 1)
            void handleEvent(Event e)
            {
                assertEquals(event, e);
                e.consume();
            }
    }.getMockInstance();

    // register the handlers and fire the event
    eventBus.addHandler(TestEvent.class, firstHandler);
    eventBus.addHandler(TestEvent.class, secondHandler);
    eventBus.fireEvent(event);

    new Verifications()
    {
        {
            // verify that the second handler was NOT notified because
            // the event was consumed by the first handler
            onInstance(secondHandler).handleEvent(event);
            times = 0;
        }
    };
}

当我尝试运行此代码时,出现以下异常:

java.lang.IllegalStateException: Missing invocation to mocked type at this 
point; please make sure such invocations appear only after the declaration
of a suitable mock field or parameter

异常发生在 times = 0 行上,我不知道为什么,因为类型 secondHandler 应该被模拟,因为它作为参数传递给测试方法。在参数中添加 @Mocked@Injectable 没有什么区别。

如果我从 firstHandler 创建一个标准类,它只会消耗事件,然后测试代码,一切都会运行得很好。但在这种情况下,我无法明确验证 firstHandler 的方法 handleEvent 是否被调用,因为它不再是模拟类型。

非常感谢任何帮助,谢谢!

I'm using the JMockit framework and I'm trying to test my simple EventBus implementation which allows EventHandlers to be registered for Event types. When an event is fired on the event bus, all registered handlers get notified. An event can be consumed by an event handler which will cause that subsequent handlers will NOT be notified of the event.

My test method looks like this:

// The parameter secondHandler should be mocked automatically by passing it
// as an argument to the test method
@Test
public void testConsumeEvent(final EventHandler<TestEvent> secondHandler)
{
    // create the event which will be fired and which the handlers are
    // listening to
    final TestEvent event = new TestEvent();

    // this handler will be called once and will consume the event
    final EventHandler<TestEvent> firstHandler = 
        new MockUp<EventHandler<TestEvent>>()
        {
            @Mock(invocations = 1)
            void handleEvent(Event e)
            {
                assertEquals(event, e);
                e.consume();
            }
    }.getMockInstance();

    // register the handlers and fire the event
    eventBus.addHandler(TestEvent.class, firstHandler);
    eventBus.addHandler(TestEvent.class, secondHandler);
    eventBus.fireEvent(event);

    new Verifications()
    {
        {
            // verify that the second handler was NOT notified because
            // the event was consumed by the first handler
            onInstance(secondHandler).handleEvent(event);
            times = 0;
        }
    };
}

When I try to run this code I get the following exception:

java.lang.IllegalStateException: Missing invocation to mocked type at this 
point; please make sure such invocations appear only after the declaration
of a suitable mock field or parameter

The exception occurs on the line times = 0 and I don't know why since the type secondHandler should be mocked because it's passed as a parameter to the test method. Adding @Mocked or @Injectable to the parameter makes no difference.

If I make a standard class from the firstHandler, which will just consume the event, and then test the code, everything runs just fine. But in that case I can't verify explicitly that the firstHandler's method handleEvent is called because it's not a mocked type anymore.

Any help is much appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2024-11-09 14:51:24

我自己找到了问题的解决方案。修复相当简单,我只需要将 Verifications 块转换为 Expectations 块,并将其放在模拟的 firstHandler 初始化之前。

在我看来,语句 new MockUp>() 模拟每种类型的 EventHandler 并覆盖已经定义的实例,即我的 第二个处理程序。我不知道我是否正确,或者这是一个错误还是一个功能。

如果有人知道到底发生了什么,请对此答案发表评论。谢谢!

I have found solution of the problem myself. The fix was rather simple, I just needed to transform the Verifications block into an Expectations block and put it BEFORE the initialization of the mocked firstHandler.

It seems to me that the statement new MockUp<EventHandler<TestEvent>>() mocks every type of EventHandler<TestEvent> and overrides already defined instances, i.e. my secondHandler. Whether I'm correct or not or whether it's a bug or a feature I don't know.

If someone knows what exactly is going on, please comment on this answer. Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文