EventProducer { public Object produceEvent() throws InvocationTargetException { if (!valid) { throw new IllegalStateException(toString() + " has been invalidated and can no longer produce events."); } try { return method.invoke(target); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { if (e.getCause() instanceof Error) { throw (Error) e.getCause(); } throw e; } } EventProducer(Object target, Method method); boolean isValid(); void invalidate(); Object produceEvent(); @Override String toString(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test public void basicMethodCall() throws Exception { Method method = getRecordingMethod(); EventProducer producer = new EventProducer(this, method); Object methodResult = producer.produceEvent(); assertTrue("Producer must call provided method.", methodCalled); assertSame("Producer result must be *exactly* the specified return value.", methodResult, FIXTURE_RETURN_VALUE); }
@Test public void testExceptionWrapping() throws NoSuchMethodException { Method method = getExceptionThrowingMethod(); EventProducer producer = new EventProducer(this, method); try { producer.produceEvent(); fail("Producers whose methods throw must throw InvocationTargetException"); } catch (InvocationTargetException e) { assertTrue("Expected exception must be wrapped.", e.getCause() instanceof IntentionalException); } }
@Test public void errorPassthrough() throws InvocationTargetException, NoSuchMethodException { Method method = getErrorThrowingMethod(); EventProducer producer = new EventProducer(this, method); try { producer.produceEvent(); fail("Producers whose methods throw Errors must rethrow them"); } catch (JudgmentError expected) { } }
@Test public void returnValueNotCached() throws Exception { Method method = getRecordingMethod(); EventProducer producer = new EventProducer(this, method); producer.produceEvent(); methodReturnValue = new Object(); methodCalled = false; Object secondReturnValue = producer.produceEvent(); assertTrue("Producer must call provided method twice.", methodCalled); assertSame("Producer result must be *exactly* the specified return value on each invocation.", secondReturnValue, methodReturnValue); }