### Question:
EurekaInstanceRegisteredListener { @EventListener public void listen(EurekaInstanceRegisteredEvent event) { final InstanceInfo instanceInfo = event.getInstanceInfo(); final Map<String, String> metadata = instanceInfo.getMetadata(); final String serviceId = EurekaUtils.getServiceIdFromInstanceId(instanceInfo.getInstanceId()); metadataTranslationService.translateMetadata(serviceId, metadata); metadataDefaultsService.updateMetadata(serviceId, metadata); if (StringUtils.equalsIgnoreCase(GatewayNotifier.GATEWAY_SERVICE_ID, serviceId)) { gatewayNotifier.distributeInvalidatedCredentials(instanceInfo.getInstanceId()); } gatewayNotifier.serviceUpdated(serviceId, instanceInfo.getInstanceId()); }  @EventListener void listen(EurekaInstanceRegisteredEvent event); @EventListener void listen(EurekaStatusUpdateEvent event);  }### Answer:
@Test public void getServiceId() { MetadataTranslationService metadataTranslationService = Mockito.mock(MetadataTranslationService.class); MetadataDefaultsService metadataDefaultsService = Mockito.mock(MetadataDefaultsService.class); GatewayNotifier gatewayNotifier = mock(GatewayNotifier.class); EurekaInstanceRegisteredListener eirl = new EurekaInstanceRegisteredListener(metadataTranslationService, metadataDefaultsService, gatewayNotifier); doAnswer( x -> { assertEquals("serviceName", x.getArgument(0)); return null; } ).when(metadataDefaultsService).updateMetadata(anyString(), any()); final Map<String, String> metadata = new HashMap<>(); InstanceInfo instanceInfo = mock(InstanceInfo.class); when(instanceInfo.getInstanceId()).thenReturn("1:serviceName:2"); EurekaInstanceRegisteredEvent event = mock(EurekaInstanceRegisteredEvent.class); when(event.getInstanceInfo()).thenReturn(instanceInfo); eirl.listen(event); verify(metadataTranslationService, times(1)).translateMetadata("serviceName", metadata); verify(metadataDefaultsService, times(1)).updateMetadata("serviceName", metadata); verify(gatewayNotifier, times(1)).serviceUpdated("serviceName", "1:serviceName:2"); }
@Test public void testListen() { GatewayNotifier notifier = mock(GatewayNotifier.class); EurekaInstanceRegisteredListener listener = new EurekaInstanceRegisteredListener(Mockito.mock(MetadataTranslationService.class), Mockito.mock(MetadataDefaultsService.class), notifier); listener.listen(createEvent("host:service:instance")); verify(notifier, times(1)).serviceUpdated("service", "host:service:instance"); listener.listen(createEvent("unknown format")); verify(notifier, times(1)).serviceUpdated(null, "unknown format"); verify(notifier, never()).distributeInvalidatedCredentials(anyString()); listener.listen(createEvent("host:GATEWAY:instance")); verify(notifier, times(1)).distributeInvalidatedCredentials("host:GATEWAY:instance"); }