### Question:
HeimdallElasticSearchRepository { public JsonArray getEventsProcessed() throws Exception { String responseJson = null; JsonParser jsonParser; JsonObject resultJson; StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append("blackbox").append("/").append("_search"); StringBuilder requestBody = new StringBuilder( "{\"size\":0,\"query\":{\"range\":{\"eventTime\":{\"gte\":\"now-1d/d\"}}},\"aggs\":{\"events-per-day\":{\"date_histogram\":{\"field\":\"eventTime\",\"interval\":\"day\",\"order\":{\"_key\":\"desc\"}}}}}"); try { responseJson = PacHttpUtils.doHttpPost(urlToQueryBuffer.toString(), requestBody.toString()); } catch (Exception e) { logger.error("error retrieving inventory from ES", e); throw new Exception(e); } jsonParser = new JsonParser(); resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get("aggregations").toString()); return aggsJson.getAsJsonObject("events-per-day").getAsJsonArray("buckets"); }  JsonArray getEventsProcessed();  }### Answer:
@Test public void getDataFromESTest() throws Exception { esUrl = "esUrl"; final HeimdallElasticSearchRepository classUnderTest = PowerMockito.spy(new HeimdallElasticSearchRepository()); ReflectionTestUtils.setField(classUnderTest, "esUrl", "esUrl123"); mockStatic(StringBuilder.class); mockStatic(PacHttpUtils.class); String response = "{\"aggregations\" :{\"events-per-day\": { \"buckets\": [{\"accountname\":{\"buckets\" :[{\"key\":\"key678\"}]},\"key\":\"key123\"},{\"accountname\":{\"buckets\" :[{\"key\":\"key566\"}]},\"key\":\"key455\"}]}}}"; PowerMockito.when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); final StringBuilder mock = PowerMockito.spy(new StringBuilder()); PowerMockito.whenNew(StringBuilder.class).withAnyArguments().thenReturn(mock); assertThat(classUnderTest.getEventsProcessed().size(), is(2)); }
@SuppressWarnings("unchecked") @Test public void getDataFromESTest1() throws Exception { esUrl = "esUrl"; final HeimdallElasticSearchRepository classUnderTest = PowerMockito.spy(new HeimdallElasticSearchRepository()); ReflectionTestUtils.setField(classUnderTest, "esUrl", "esUrl123"); mockStatic(StringBuilder.class); mockStatic(PacHttpUtils.class); PowerMockito.when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(Exception.class); final StringBuilder mock = PowerMockito.spy(new StringBuilder()); PowerMockito.whenNew(StringBuilder.class).withAnyArguments().thenReturn(mock); assertThatThrownBy(() -> classUnderTest.getEventsProcessed()).isInstanceOf(Exception.class); }