### Question:
WorkspaceController { @EventListener public CompletableFuture<Void> loadWorspace(LoadWorkspaceEvent event) { LOG.debug(DefaultI18nContext.getInstance().i18n("Loading workspace from {0}", event.workspace().getName())); return CompletableFuture.supplyAsync(() -> service.loadWorkspace(event.workspace())).thenCompose((data) -> { if (!data.isEmpty()) { event.setData(data); return CompletableFuture.allOf(modules.stream() .map(m -> CompletableFuture.runAsync(() -> eventStudio().broadcast(event, m.id()))) .toArray(CompletableFuture[]::new)).thenRun(() -> { recentWorkspace.addWorkspaceLastUsed(event.workspace()); eventStudio().broadcast(new WorkspaceLoadedEvent(event.workspace())); LOG.info(DefaultI18nContext.getInstance().i18n("Workspace loaded")); }); } return CompletableFuture.completedFuture(null); }).whenComplete((r, e) -> { if (nonNull(e)) { LOG.error(DefaultI18nContext.getInstance().i18n("Unable to load workspace from {0}", event.workspace().getName()), e); } }); } @Inject  WorkspaceController(List<Module> modules, WorkspaceService service, RecentWorkspacesService recentWorkspace); @EventListener void saveWorkspace(SaveWorkspaceEvent event); @EventListener CompletableFuture<Void> loadWorspace(LoadWorkspaceEvent event);  }### Answer:
@Test public void loadEmptyWorkspace() throws InterruptedException, ExecutionException { Listener<LoadWorkspaceEvent> listener = mock(Listener.class); eventStudio().add(LoadWorkspaceEvent.class, listener, DefaultPriorityTestModule.ID); when(service.loadWorkspace(any())).thenReturn(Collections.emptyMap()); CompletableFuture<Void> future = victim.loadWorspace(new LoadWorkspaceEvent(file)); future.get(); verify(listener, never()).onEvent(any()); }
@Test(expected = ExecutionException.class) public void loadWorkspaceWithException() throws InterruptedException, ExecutionException { Listener<LoadWorkspaceEvent> listener = mock(Listener.class); eventStudio().add(LoadWorkspaceEvent.class, listener, DefaultPriorityTestModule.ID); when(service.loadWorkspace(eq(file))).thenThrow(new RuntimeException("mock")); CompletableFuture<Void> future = victim.loadWorspace(new LoadWorkspaceEvent(file)); future.get(); }
@Test public void loadWorkspace() throws InterruptedException, ExecutionException { Listener<LoadWorkspaceEvent> listener = mock(Listener.class); eventStudio().add(LoadWorkspaceEvent.class, listener, DefaultPriorityTestModule.ID); Listener<WorkspaceLoadedEvent> loadedListener = mock(Listener.class); eventStudio().add(WorkspaceLoadedEvent.class, loadedListener); Map<String, Map<String, String>> data = new HashMap<>(); Map<String, String> moduleData = new HashMap<>(); moduleData.put("key", "value"); data.put("module", moduleData); when(service.loadWorkspace(any())).thenReturn(data); CompletableFuture<Void> future = victim.loadWorspace(new LoadWorkspaceEvent(file)); future.get(); verify(listener).onEvent(any()); verify(recentWorkspaces).addWorkspaceLastUsed(file); verify(loadedListener).onEvent(any()); }
@Test public void loadWorkspaceNoDataForModule() throws InterruptedException, ExecutionException { Listener<LoadWorkspaceEvent> listener = mock(Listener.class); eventStudio().add(LoadWorkspaceEvent.class, listener, "anotherModule"); Map<String, Map<String, String>> data = new HashMap<>(); Map<String, String> moduleData = new HashMap<>(); moduleData.put("key", "value"); data.put("anotherModule", moduleData); when(service.loadWorkspace(any())).thenReturn(data); CompletableFuture<Void> future = victim.loadWorspace(new LoadWorkspaceEvent(file)); future.get(); verify(listener, never()).onEvent(any()); verify(recentWorkspaces).addWorkspaceLastUsed(any()); }