Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Below is Java code.

I need to cover the below function. I searched for many websites, but I still have no ideas. Is there any methods to cover the override anonymous classes?

  public static void addEnterListener(Text text, final String methodName, final Object callee) {
    text.addKeyListener(new KeyListener() {
    @Override
    public void keyReleased(KeyEvent arg0) {
    if (arg0.keyCode == '\r') {
      try {
        Method method = callee.getClass().getMethod(methodName, KeyEvent.class);
        method.invoke(callee, arg0);
      } catch (Exception e) {e.printStackTrace();}
    }
  }

  @Override
  public void keyPressed(KeyEvent arg0) { 
  }
});
}
share|improve this question
    
take a look at the answers to this SO question: Access anonymous inner class variables –  gnat Feb 28 '13 at 17:57
add comment

3 Answers 3

No, there is no way to do that with annonymous classes.

You can refactorize your code so:

  public static void addEnterListener(Text text, final String methodName, final Object callee) {
    text.addKeyListener(new MyKeyListener());
  }

  class MyKeyListener implements KeyListener {
    @Override
    public void keyReleased(KeyEvent arg0) {
      if (arg0.keyCode == '\r') {
        try {
          Method method = callee.getClass().getMethod(methodName, KeyEvent.class);
          method.invoke(callee, arg0);
        } catch (Exception e) {e.printStackTrace();}
      }
    }

    @Override
    public void keyPressed(KeyEvent arg0) { 
    }
  }
share|improve this answer
    
If I am not allowed to change the addEnterListener, that really means no approach to invoke it?? Because I have invoked the similar codes accidentally with a lot of error message. But, I still dont know how to cover these function –  wonderland Feb 27 '13 at 9:20
add comment

Well, for THAT code, you could inject a mocked Text-object and fetch the keyListener after return and call keyReleased on this, e.g. using mockito

@Test
void doTest(){
  Text mock = mock(Text.class);
  StaticClass.addEnterListener(mock, ....);
  ArgumentCaptor<KeyListener> captor = ArgumentCaptor.forClass(KeyListener.class);
  verify(mock).addKeyListener(captor.capture());
  KeyListener theListener = captor.getValue();

  theListener.keyReleased(...);
}

I did not test this, but this should be the way... though i would encourage to refactor this...

share|improve this answer
add comment

As a third option, refactor creation of the KeyListener into a getKeyListener method. Call that method from your test code to get a KeyListener to test. Also call it in addEnterListener to get the KeyListener to add.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.