title: How to test an anonymous inner class that calls a private method
body: <p>We have a bunch of classes that listen for events from the server and then respond to them. For example:</p>
  
  <pre><code>class EventManager {
      private Set&lt;Event&gt; cache = new HashSet&lt;Event&gt;();
  
      private EventListener eventListener = new EventListener() {
          void onEvent(Event e) {
            if (e instanceof MyEvent || e instanceof YourEvent) {
              handleEvent(e);
            }  
          }
      }
  
      public EventManager(ServerCommunication serverComm) {
          serverComm.addListener(eventListener);
      }
  
      private handleEvent(Event e) {
          // handle the event...
          // ...
          cache.add(cache);
          // ...
      }
  }
  </code></pre>
  
  <p>Here's a made-up example of the kind of thing we are doing. Here are the problems I see:</p>
  
  <ol>
  <li>I'd like to test handleEvent to make sure it's doing what it is supposed to but I can't because it's private.</li>
  <li>I'd also like to check that something got added to the cache too but that also seems difficult since cache is a private member and I don't want to add a needless getter method.</li>
  <li>I'd also like to test the code inside the anonymous class's onEvent method.</li>
  </ol>
  
  <p>For now, what I did was move all logic from the anonymous class to the handleEvent method, and I made handleEvent package private (my unit test is in the same package). I'm not checking the contents of the cache although I want to.</p>
  
  <p>Does anyone have any suggestion for a better design that is more testable?</p>