ViewScope implements Scope { @Override public void registerDestructionCallback(String name, Runnable callback) { DestructionCallbackWrapper wrapper = new DestructionCallbackWrapper(name, callback); getFacesContext().getApplication().subscribeToEvent(PreDestroyViewMapEvent.class, this.preDestroyViewMapListener); getViewRoot().getViewMap().put(DESTRUCTION_CALLBACK_NAME_PREFIX + name, wrapper); getSessionListener().register(wrapper); }  @Override Object get(String name, ObjectFactory objectFactory); @Override @Nullable Object remove(String name); @Override @Nullable String getConversationId(); @Override void registerDestructionCallback(String name, Runnable callback); @Override @Nullable Object resolveContextualObject(String key); static final String SCOPE_VIEW; static final String DESTRUCTION_CALLBACK_NAME_PREFIX; }
@Test public void testRegisterDestructionCallback() { UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); doCallRealMethod().when(viewRoot).subscribeToViewEvent(any(), any()); doCallRealMethod().when(viewRoot).unsubscribeFromViewEvent(any(), any()); when(viewRoot.getViewListenersForEventClass(any())).thenCallRealMethod(); this.viewScope.registerDestructionCallback(KEY, () -> { }); this.viewScope.remove(KEY); }