コード例 #1
0
  /**
   * Use forEach to change the color, and build a string showing the old colors of each shape.
   *
   * <p>Try to perform both actions using a single call to forEach.
   *
   * @see Shapes#changeColorAndMakeStringOfOldColors
   * @see Shape#getColor()
   * @see Color#name()
   * @see StringBuilder#append(String)
   */
  @Test
  public void changeColorOfAllShapes_AND_buildStringShowingAllTheOldColors() {
    List<Shape> myShapes = Arrays.asList(new Shape(BLUE), new Shape(BLACK), new Shape(YELLOW));
    StringBuilder builder = new StringBuilder();

    Shapes.changeColorAndMakeStringOfOldColors(myShapes, RED, builder);

    assertThat(myShapes, hasSize(3));
    assertThat(myShapes, everyItem(hasColor(RED)));
    assertThat(builder.toString(), equalTo("[a BLUE shape][a BLACK shape][a YELLOW shape]"));
  }