How to check an anonymous inner class that calls a private method

We have a bunch of classes that listen for events from the server and then respond to them. For instance:

class EventManager {
    private Set<Event> cache = new HashSet<Event>();

    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);
        // ...
    }
}

Here is an example of what we are doing. Here are the problems I see:

  • I would like to check handleEvent to make sure that it does what it should, but I cannot because it is closed.
  • I would also like to check that something has been added to the cache, but it also seems difficult, since the cache is a private member and I do not want to add an unnecessary getter method.
  • I would also like to test the code inside the method of the anonymous onEvent class.

handleEvent, handleEvent ( unit test ). , .

- , ?

+3
5

, , EventCache. , , .

, , handleEvent. ServerCommunication, .

+3

, : .

, . - ? ( - , , .) , . , , - .

, , , ,

Cache getCacheForTesting()

" " , , . , , , , , , .

- , , IMO. , , , . API.

+2

, EventManager , .

1 - , . , , .

2 - , . .

3 - ? , , EventListener EventListener ( onEvent ). , , , .

+1

, , ... , .

, HandleEvent ( onEvent), HandleEvent / .

+1

, ( ). - .

, onEvent , .

0

Source: https://habr.com/ru/post/1754948/


All Articles