MeterProcessor { public void process() { if (StringUtils.isEmpty(service) || StringUtil.isEmpty(serviceInstance) || timestamp == null) { return; } final List<MeterBuilder> enabledBuilders = processService.enabledBuilders(); if (CollectionUtils.isEmpty(enabledBuilders)) { return; } try { final Binding binding = new Binding(); binding.setVariable("meter", new BindingMeterMap(meters)); final GroovyShell shell = new GroovyShell(binding); for (MeterBuilder builder : enabledBuilders) { builder.buildAndSend(this, shell); } } catch (Exception e) { log.warn("Process meters failure.", e); } } MeterProcessor(MeterProcessService processService); void read(MeterData data); void process();  }
@Test public void testProcess() { MeterProcessService context = (MeterProcessService) Whitebox.getInternalState(processor, "processService"); List<MeterBuilder> builders = context.enabledBuilders() .stream() .map(Mockito::spy) .peek(builder -> doNothing().when(builder).buildAndSend(any(), any())) .collect(Collectors.toList()); Whitebox.setInternalState(context, "meterBuilders", builders); processor.process(); builders.stream().forEach(b -> verify(b, times(1)).buildAndSend(any(), any())); Whitebox.setInternalState(processor, "service", ""); processor.process(); builders.stream().forEach(b -> verify(b, times(0)).buildAndSend(any(), any())); Whitebox.setInternalState(processor, "service", "service"); context = Mockito.spy(context); when(context.enabledBuilders()).thenReturn(Collections.emptyList()); processor.process(); builders.stream().forEach(b -> verify(b, times(0)).buildAndSend(any(), any())); }