### Question:
AbstractMetricPublisher extends AbstractTimelineMetricsSink implements Runnable { protected void processAndPublishMetrics(Map<String, TimelineMetrics> metricsFromCache) throws Exception { if (metricsFromCache.size()==0) return; LOG.info(String.format("Preparing %s timeline metrics for publishing", metricsFromCache.size())); emitMetricsJson(getCollectorUri(getCurrentCollectorHost()), processMetrics(metricsFromCache)); } AbstractMetricPublisher(TimelineMetricsHolder timelineMetricsHolder, Configuration configuration, int publishIntervalInSeconds); @Override void run();  }### Answer:
@Test public void testProcessAndPublishMetrics() throws Exception { AbstractMetricPublisher publisherMock = createMockBuilder(RawMetricsPublisher.class) .withConstructor(TimelineMetricsHolder.getInstance(), new Configuration(), 60) .addMockedMethod("processMetrics") .addMockedMethod("getCollectorUri") .addMockedMethod("emitMetricsJson") .addMockedMethod("getCurrentCollectorHost").createStrictMock(); TimelineMetricsHolder.getInstance().putMetricsForRawPublishing(TimelineMetricsHolderTest.getTimelineMetricsWithAppID("raw")); expect(publisherMock.getCurrentCollectorHost()).andReturn("collectorhost").once(); expect(publisherMock.getCollectorUri(anyString())).andReturn("https: expect(publisherMock.processMetrics(anyObject(Map.class))).andReturn("{metrics}").once(); expect(publisherMock.emitMetricsJson("https: replay(publisherMock); publisherMock.processAndPublishMetrics(TimelineMetricsHolder.getInstance().extractMetricsForRawPublishing()); verify(publisherMock); }
@Test public void testRunAndStop() throws Exception { AbstractMetricPublisher publisherMock = createMockBuilder(RawMetricsPublisher.class) .withConstructor(TimelineMetricsHolder.getInstance(), new Configuration(), 1) .addMockedMethod("processAndPublishMetrics").createStrictMock(); publisherMock.processAndPublishMetrics(anyObject(Map.class)); expectLastCall().times(1); Thread t = createMockBuilder(Thread.class) .withConstructor(publisherMock) .addMockedMethod("isInterrupted").createStrictMock(); expect(t.isInterrupted()).andReturn(false).once(); expect(t.isInterrupted()).andReturn(true).once(); replay(publisherMock, t); t.start(); Thread.sleep(2222); verify(publisherMock, t); }