title: JMockit - two mocked instances of the same type
body: <p>I'm using the JMockit framework and I'm trying to test my simple <code>EventBus</code> implementation which allows <code>EventHandlers</code> to be registered for <code>Event</code> types. When an event is <code>fired</code> on the event bus, all registered handlers get notified. An event can be <em>consumed</em> by an event handler which will cause that subsequent handlers will NOT be notified of the event.</p>
  
  <p>My test method looks like this:</p>
  
  <pre><code>// The parameter secondHandler should be mocked automatically by passing it
  // as an argument to the test method
  @Test
  public void testConsumeEvent(final EventHandler&lt;TestEvent&gt; 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&lt;TestEvent&gt; firstHandler = 
          new MockUp&lt;EventHandler&lt;TestEvent&gt;&gt;()
          {
              @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;
          }
      };
  }
  </code></pre>
  
  <p>When I try to run this code I get the following exception:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The exception occurs on the line <code>times = 0</code> and I don't know why since the type <code>secondHandler</code> should be mocked because it's passed as a parameter to the test method. Adding <code>@Mocked</code> or <code>@Injectable</code> to the parameter makes no difference.</p>
  
  <p>If I make a standard class from the <code>firstHandler</code>, 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 <code>firstHandler</code>'s method <code>handleEvent</code> is called because it's not a mocked type anymore.</p>
  
  <p>Any help is much appreciated, thanks!</p>