### Question:
ConfigService { public String getConfigAsJson(String name) { String rc = "{}"; try { Optional<AcmConfig> optionalRcString = configList.stream().filter(x -> x.getConfigName().equals(name)).findFirst(); if (optionalRcString.isPresent()) rc = optionalRcString.get().getConfigAsJson(); } catch (NoSuchElementException e) { log.error(e.getMessage()); } return rc; }  List<Map<String, String>> getInfo(); String getConfigAsJson(String name); List<AcmConfig> getConfigList(); void setConfigList(List<AcmConfig> configList);  }### Answer:
@Test public void getConfig1() throws Exception { expect(mockConfig1.getConfigName()).andReturn("config1"); expect(mockConfig1.getConfigAsJson()).andReturn("{\"some1\":\"value1\"}"); replayAll(); String returned = unit.getConfigAsJson("config1"); verifyAll(); assertNotNull(returned); ObjectMapper om = new ObjectMapper(); JsonNode actualObj = om.readTree(returned); JsonNode someNode = actualObj.path("some1"); String someValue = someNode.textValue(); assertEquals(someValue, "value1"); }
@Test public void getConfig2() throws Exception { expect(mockConfig1.getConfigName()).andReturn("config1"); expect(mockConfig2.getConfigName()).andReturn("config2"); expect(mockConfig2.getConfigAsJson()).andReturn("{\"some2\":\"value2\"}"); replayAll(); String returned = unit.getConfigAsJson("config2"); verifyAll(); assertNotNull(returned); ObjectMapper om = new ObjectMapper(); JsonNode actualObj = om.readTree(returned); JsonNode someNode = actualObj.path("some2"); String someValue = someNode.textValue(); assertEquals(someValue, "value2"); }
@Test public void getConfig3() throws Exception { expect(mockConfig1.getConfigName()).andReturn("config1"); expect(mockConfig2.getConfigName()).andReturn("config2"); expect(mockConfig3.getConfigName()).andReturn("config3"); expect(mockConfig3.getConfigAsJson()).andReturn("{\"some3\":\"value3\"}"); replayAll(); String returned = unit.getConfigAsJson("config3"); verifyAll(); assertNotNull(returned); ObjectMapper om = new ObjectMapper(); JsonNode actualObj = om.readTree(returned); JsonNode someNode = actualObj.path("some3"); String someValue = someNode.textValue(); assertEquals(someValue, "value3"); }
@Test public void getConfig4() throws Exception { expect(mockConfig1.getConfigName()).andReturn("config1"); expect(mockConfig2.getConfigName()).andReturn("config2"); expect(mockConfig3.getConfigName()).andReturn("config3"); replayAll(); String returned = unit.getConfigAsJson("config_no_such"); verifyAll(); assertEquals(returned, "{}"); }