### Question:
DiagnosticContext { public void printContextInformation(StringBuilder result) { String contextInformation = context.entrySet().stream() .filter(e -> Objects.nonNull(e.getValue())) .filter(e -> !e.getValue().isEmpty()) .map(es -> es.getKey() + "=" + es.getValue()) .collect(joining(";")); result.append(contextInformation); if (!contextInformation.isEmpty()) { result.append(","); } } DiagnosticContext(DiagnosticContextSupplier supplier); Map<String, String> getContext(); void printContextInformation(StringBuilder result);  }### Answer:
@Test public void printContextInformation_shouldPrintNameValuePairsSeparatedBySemiColonsInTheCorrectOrderFollowedByAComma() throws Exception { Map<String, String> context = new LinkedHashMap<>(); context.put("baker", "a"); context.put("able", "b"); context.put("charlie", "c"); StringBuilder sb = new StringBuilder(); new DiagnosticContext(() -> context).printContextInformation(sb); assertEquals(sb.toString(), "baker=a;able=b;charlie=c,"); }
@Test public void printContextInformation_shouldExcludeMappingsWithEmptyValues() throws Exception { Map<String, String> context = new LinkedHashMap<>(); context.put("baker", "a"); context.put("able", ""); context.put("charlie", "c"); StringBuilder sb = new StringBuilder(); new DiagnosticContext(() -> context).printContextInformation(sb); assertEquals(sb.toString(), "baker=a;charlie=c,"); }
@Test public void printContextInformation_shouldExcludeMappingsWithNullValues() throws Exception { Map<String, String> context = new LinkedHashMap<>(); context.put("baker", "a"); context.put("able", null); context.put("charlie", "c"); StringBuilder sb = new StringBuilder(); new DiagnosticContext(() -> context).printContextInformation(sb); assertEquals(sb.toString(), "baker=a;charlie=c,"); }
@Test public void printContextInformation_shouldNotPrintATrailingComma_givenAnEffectivelyEmptyMergedContext() throws Exception { Map<String, String> emptyValue = new LinkedHashMap<>(); emptyValue.put("able", ""); Map<String, String> nullValue = new LinkedHashMap<>(); nullValue.put("able", null); StringBuilder sb = new StringBuilder(); new DiagnosticContext(() -> emptyValue).printContextInformation(sb); new DiagnosticContext(() -> nullValue).printContextInformation(sb); new DiagnosticContext(() -> null).printContextInformation(sb); new DiagnosticContext(null).printContextInformation(sb); assertEquals(sb.toString(), ""); }
@Test public void constructor_shouldPreserveInsertionOrder() throws Exception { Map<String, String> context = new LinkedHashMap<>(); context.put("baker", "a"); context.put("able", "b"); context.put("charlie", "c"); StringBuilder sb = new StringBuilder(); new DiagnosticContext(() -> context).printContextInformation(sb); assertEquals("baker=a;able=b;charlie=c,", sb.toString()); }