예제 #1
0
  @Test
  public void testSmartTroll() throws Exception {
    // Create a normal troll first, but make sure we can spy on it later on.
    final Hostile simpleTroll = spy(new Troll());

    // Now we want to decorate the troll to make it smarter ...
    final Hostile smartTroll = new SmartTroll(simpleTroll);
    assertEquals(30, smartTroll.getAttackPower());
    verify(simpleTroll, times(1)).getAttackPower();

    // Check if the smart troll actions are delegated to the decorated troll
    smartTroll.attack();
    verify(simpleTroll, times(1)).attack();

    smartTroll.fleeBattle();
    verify(simpleTroll, times(1)).fleeBattle();
    verifyNoMoreInteractions(simpleTroll);
  }
예제 #2
0
 @Override
 public void fleeBattle() {
   System.out.println("The troll calls for help!");
   decorated.fleeBattle();
 }
예제 #3
0
 @Override
 public void attack() {
   System.out.println("The troll throws a rock at you!");
   decorated.attack();
 }