Listeners

I need unit test event listener functionality, but I have never done this before, and I cannot find an example anywhere about this. Does anyone have any suggestions on a good way to go about this?

+4
source share
1 answer

There is not much there, build an event listener, pass in the mock event and test.

@Test
public void testEventListener() {
  ActionListener subjectUnderTest = new MyActionListener();
  ActionEvent mockEvent = mock(ActionEvent.class);
  // Or just create a new ActionEvent, e.g. new ActionEvent();
  // setup mock

  subjectUnderTest.actionPerformed(mockEvent);

  // validate
}

, , , . - , .

+5

All Articles