@Test
  public void constructorWorksWhenNonNullFactoryProvided() {

    KeystrokeFactory mockFactory = createMock(KeystrokeFactory.class);
    replayMock(mockFactory);
    KeystrokeRecorderImpl recorder = new KeystrokeRecorderImpl(mockFactory);
    assertNotNull(recorder);
  }
  private KeystrokeFactory createMockFactoryExpecting(
      char expectedChar, long expectedTime, Keystroke returnValue) {

    KeystrokeFactory mockFactory = createMock(KeystrokeFactory.class);

    expect(mockFactory.createKeystroke(expectedChar, expectedTime)).andReturn(returnValue).once();

    expect(mockFactory.acceptsCharacter('a')).andReturn(true);
    replayMock(mockFactory);

    return mockFactory;
  }
  @Test
  public void keyUpReleasesCreatedCharacter() {

    long releaseTime = testStart + 1000;

    Keystroke mockKeystroke = createMock(Keystroke.class);

    expect(mockKeystroke.getValue()).andReturn(testChar).anyTimes();

    mockKeystroke.release(releaseTime);
    expectLastCall().once();
    replayMock(mockKeystroke);

    KeystrokeFactory mockFactory = createMockFactoryExpecting(testChar, testStart, mockKeystroke);

    KeystrokeRecorder recorder = new KeystrokeRecorderImpl(mockFactory);

    recorder.keyDown(testChar, testStart);
    recorder.keyUp(testChar, releaseTime);
  }