, unittest ApplicationPanel Listener, Listener ApplicationPanel ( Listener, , ApplicationPanel). mock Listener , , .
, Listener ApplicationPanel ApplicationPanel. . , Listener ApplicationPanel, , ApplicationPanel !
Instead, pass data to the listener when it receives events:
public interface Listener {
void onApplicationChanged(ApplicationPanel panel){
}
}
A more traditional way to add a listener to an object that dispatches events is to call a method:
public class ApplicationPanel {
private List<Listener> listeners = new CopyOnWriteArrayList<Listener>();
public void addListener(Listener listener) {
listeners.add(listener);
}
}
If you want a class that dispatches events to use the constructor to specify listeners, and you want to use Guice, check out Multibinder . ApplicationPanelwill look like this:
public class ApplicationPanel {
private Set<Listener> listeners;
@Inject
public ApplicationPanel(Set<Listener> listeners) {
listeners = new HashSet<Listener>(listeners);
}
}
source
share