JMockit-两个相同类型的模拟实例 [英] JMockit - two mocked instances of the same type

查看:115
本文介绍了JMockit-两个相同类型的模拟实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JMockit框架,并且试图测试我的简单EventBus实现,该实现允许将EventHandlers注册为Event类型.当事件在事件总线上为fired时,将通知所有已注册的处理程序.事件处理程序可以消耗一个事件,这将导致以后的处理程序不会收到该事件的通知.

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.

我的测试方法如下:

// 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

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

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.

如果我从firstHandler中创建一个标准类,它将仅使用该事件,然后测试代码,则一切运行正常.但是在那种情况下,我无法明确验证firstHandler的方法handleEvent是否已被调用,因为它不再是模拟类型了.

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.

非常感谢您的帮助!

推荐答案

我本人已找到解决问题的方法.修复非常简单,我只需要将Verifications块转换为Expectations块,然后将其放在模拟的firstHandler初始化之前即可.

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.

在我看来,语句new MockUp<EventHandler<TestEvent>>()模拟每种类型的EventHandler<TestEvent>并覆盖已定义的实例,即我的secondHandler.我是否正确,或者是我不知道的错误还是功能.

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!

这篇关于JMockit-两个相同类型的模拟实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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