Source:
<code>public void testEvents() { doAnswer(invocation -> { Firmware firmware = (Firmware) invocation.getArguments()[0]; ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1]; progressCallback.defineSequence(SEQUENCE); progressCallback.next(); progressCallback.next(); progressCallback.next(); progressCallback.next(); thing1.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion()); return null; }).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class)); assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112)); firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getUID(), null); AtomicReference<List<Event>> events = new AtomicReference<>(new ArrayList<>()); ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class); waitForAssert(() -> { verify(mockPublisher, atLeast(SEQUENCE.length)).post(eventCaptor.capture()); }); events.get().addAll(eventCaptor.getAllValues()); List<Event> list = events.get().stream().filter(event -> event instanceof FirmwareUpdateProgressInfoEvent) .collect(Collectors.toList()); <START> waitForAssert(() -> { <END> assertTrue(list.size() >= SEQUENCE.length); }); for (int i = 0; i < SEQUENCE.length; i++) { FirmwareUpdateProgressInfoEvent event = (FirmwareUpdateProgressInfoEvent) list.get(i); assertThat(event.getTopic(), containsString(THING1_UID.getAsString())); assertThat(event.getThingUID(), is(THING1_UID)); assertThat(event.getProgressInfo().getProgressStep(), is(SEQUENCE[i])); } }</code><technical_language>I doubt this fix test. waiting in case event list correct size. is background thread modify list waiting. assumption is lines 644 - 649 problem: waitForAssert in line 644 verifies atLeast SEQUENCE.length times, (line 648) some of captured events filtered out. If this is case list shorter SEQUENCE.length. wdyt</technical_language>

Target:
public void testEvents() { doAnswer(invocation -> { Firmware firmware = (Firmware) invocation.getArguments()[0]; ProgressCallback progressCallback = (ProgressCallback) invocation.getArguments()[1]; progressCallback.defineSequence(SEQUENCE); progressCallback.next(); progressCallback.next(); progressCallback.next(); progressCallback.next(); thing1.setProperty(Thing.PROPERTY_FIRMWARE_VERSION, firmware.getVersion()); return null; }).when(handler1).updateFirmware(any(Firmware.class), any(ProgressCallback.class)); assertThat(firmwareUpdateService.getFirmwareStatusInfo(THING1_UID), is(updateExecutableInfoFw112)); firmwareUpdateService.updateFirmware(THING1_UID, FW112_EN.getUID(), null); AtomicReference<List<Event>> events = new AtomicReference<>(new ArrayList<>()); ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class); waitForAssert(() -> { verify(mockPublisher, atLeast(SEQUENCE.length + 1)).post(eventCaptor.capture()); }); events.get().addAll(eventCaptor.getAllValues()); List<Event> list = events.get().stream().filter(event -> event instanceof FirmwareUpdateProgressInfoEvent) .collect(Collectors.toList()); assertTrue(list.size() >= SEQUENCE.length); for (int i = 0; i < SEQUENCE.length; i++) { FirmwareUpdateProgressInfoEvent event = (FirmwareUpdateProgressInfoEvent) list.get(i); assertThat(event.getTopic(), containsString(THING1_UID.getAsString())); assertThat(event.getThingUID(), is(THING1_UID)); assertThat(event.getProgressInfo().getProgressStep(), is(SEQUENCE[i])); } }