### Question:
CompositeException extends RuntimeException { public List<Throwable> getCauses() { List<Throwable> causes = new ArrayList<>(); causes.add(getCause()); causes.addAll(Arrays.asList(getSuppressed())); return causes; } CompositeException(List<Throwable> causes);  CompositeException(Throwable... causes);  CompositeException(CompositeException other, Throwable toBeAppended); @Override String getMessage(); List<Throwable> getCauses();  }### Answer:
@Test public void testCreationWithArrays() { CompositeException ce1 = new CompositeException(IAE, IO, OUPS); CompositeException ce2 = new CompositeException(IO, IAE, OUPS); assertThat(ce1).hasMessageContaining("iae").hasMessageContaining("io").hasMessageContaining("oups"); assertThat(ce1).getCause().isEqualTo(IAE); assertThat(ce2).getCause().isEqualTo(IO); assertThat(ce1.getCauses()).containsExactly(IAE, IO, OUPS); assertThat(ce1.getSuppressed()).containsExactly(IO, OUPS); assertThat(ce2.getCauses()).containsExactly(IO, IAE, OUPS); assertThat(ce2.getSuppressed()).containsExactly(IAE, OUPS); CompositeException ce3 = new CompositeException(OUPS); assertThat(ce3).hasMessageContaining("oups"); assertThat(ce3.getCauses()).containsExactly(OUPS); assertThat(ce3.getCause()).isEqualTo(OUPS); assertThat(ce3.getSuppressed()).isEmpty(); }
@Test public void testCreationWithList() { CompositeException ce1 = new CompositeException(Arrays.asList(IAE, IO, OUPS)); CompositeException ce2 = new CompositeException(Arrays.asList(IO, IAE, OUPS)); assertThat(ce1).hasMessageContaining("iae").hasMessageContaining("io").hasMessageContaining("oups"); assertThat(ce1).getCause().isEqualTo(IAE); assertThat(ce2).getCause().isEqualTo(IO); assertThat(ce1.getCauses()).containsExactly(IAE, IO, OUPS); assertThat(ce1.getSuppressed()).containsExactly(IO, OUPS); assertThat(ce2.getCauses()).containsExactly(IO, IAE, OUPS); assertThat(ce2.getSuppressed()).containsExactly(IAE, OUPS); CompositeException ce3 = new CompositeException(Collections.singletonList(OUPS)); assertThat(ce3).hasMessageContaining("oups"); assertThat(ce3.getCauses()).containsExactly(OUPS); assertThat(ce3.getCause()).isEqualTo(OUPS); assertThat(ce3.getSuppressed()).isEmpty(); }
@Test public void testCreationFromAnExisting() { CompositeException ce1 = new CompositeException(Arrays.asList(IAE, IO, OUPS)); Exception another = new Exception("another"); CompositeException ce2 = new CompositeException(ce1, another); assertThat(ce2.getCauses()).containsExactly(IAE, IO, OUPS, another); assertThat(ce2.getCause()).isEqualTo(IAE); }
@Test public void testWithASingleException() { CompositeException ce = new CompositeException(IAE); assertThat(ce.getCauses()).hasSize(1); assertThat(ce.getCause()).isEqualTo(IAE); assertThat(ce.getSuppressed()).isEmpty(); }