### Question:
PremiumModulesController { @EventListener public void fetchPremium(FetchPremiumModulesRequest event) { LOG.debug(DefaultI18nContext.getInstance().i18n("Fetching premium modules")); CompletableFuture.supplyAsync(service::getPremiumModules).thenAcceptAsync(premiumModules -> { if (nonNull(premiumModules) && !premiumModules.isEmpty()) { eventStudio().broadcast(new PremiumModulesEvent(premiumModules)); } }).whenComplete((r, e) -> { if (nonNull(e)) { LOG.warn(DefaultI18nContext.getInstance().i18n("Unable to retrieve premium modules"), e); } }); } @Inject  PremiumModulesController(PremiumModulesService service); @EventListener void fetchPremium(FetchPremiumModulesRequest event);  }### Answer:
@Test public void fetchPremiumModules() { PremiumModule data = new PremiumModule(); when(service.getPremiumModules()).thenReturn(Arrays.asList(data)); Listener<PremiumModulesEvent> listener = mock(Listener.class); eventStudio().add(PremiumModulesEvent.class, listener); victim.fetchPremium(FetchPremiumModulesRequest.INSTANCE); verify(service, timeout(1000).times(1)).getPremiumModules(); ArgumentCaptor<PremiumModulesEvent> captor = ArgumentCaptor.forClass(PremiumModulesEvent.class); verify(listener, timeout(1000).times(1)).onEvent(captor.capture()); assertEquals(1, captor.getValue().premiumModules.size()); assertEquals(data, captor.getValue().premiumModules.get(0)); }
@Test public void emptyFetchNews() { when(service.getPremiumModules()).thenReturn(Collections.emptyList()); Listener<PremiumModulesEvent> listener = mock(Listener.class); eventStudio().add(PremiumModulesEvent.class, listener); victim.fetchPremium(FetchPremiumModulesRequest.INSTANCE); verify(service, timeout(1000).times(1)).getPremiumModules(); verify(listener, after(1000).never()).onEvent(any()); }
@Test public void nullFetchNews() { when(service.getPremiumModules()).thenReturn(null); Listener<PremiumModulesEvent> listener = mock(Listener.class); eventStudio().add(PremiumModulesEvent.class, listener); victim.fetchPremium(FetchPremiumModulesRequest.INSTANCE); verify(service, timeout(1000).times(1)).getPremiumModules(); verify(listener, after(1000).never()).onEvent(any()); }
@Test public void failingFetchNews() { when(service.getPremiumModules()).thenThrow(new RuntimeException()); Listener<PremiumModulesEvent> listener = mock(Listener.class); eventStudio().add(PremiumModulesEvent.class, listener); victim.fetchPremium(FetchPremiumModulesRequest.INSTANCE); verify(service, timeout(1000).times(1)).getPremiumModules(); verify(listener, after(1000).never()).onEvent(any()); }