How to write testable code

How to write testable code

B59da5a954e84edb84394ce1d72e3567?s=128

perforb

June 04, 2018
Tweet

Transcript

  1. 0 40 2 4 4 4 / 0 1 2

    02
  2.  $,2*/%-,.+3/#'"#  $  &  $ (13,!&

  3. 2 2 1 32

  4. U 2 U 1 . 3 J

  5. .

  6. . public class Fight { private State state = new

    State(); private Player player1, player2; public Fight(Player player1, Player player2) { this.player1 = player1; this.player2 = player2; } ... public void attack(Player attacker, Weapon weapon) { Player opponent = getOpponentOf(attacker); int damage = calculateDamage(opponent, weapon); opponent.setHp(opponent.getHp() - damage); Utility.updatePlayer(opponent); updateState(); } public State getState() { return state; } ... }
  7. . public class FightTest { @Test public void testAttack() {

    Player player1 = new Player(); Player player2 = new Player(); Weapon weapon = new Weapon(); Fight fight = new Fight(player1, player2); fight.attack(player1, weapon); fight.attack(player1, weapon); fight.attack(player1, weapon); State state = fight.getState(); assertThat(state.isFinished(), is(true)); } }
  8. 

  9. public class Fight { private State state = new State();

    private Player player1, player2; public Fight(Player player1, Player player2) { this.player1 = player1; this.player2 = player2; } ... public void attack(Player attacker, Weapon weapon) { Player opponent = getOpponentOf(attacker); int damage = calculateDamage(opponent, weapon); opponent.setHp(opponent.getHp() - damage); Utility.updatePlayer(opponent); updateState(); } public State getState() { return state; } ... } .
  10. B . D public class Fight { private State state

    = new State(); private Player player1, player2; public Fight(Player player1, Player player2) { this.player1 = player1; this.player2 = player2; } ... public void attack(Player attacker, Weapon weapon) { Player opponent = getOpponentOf(attacker); int damage = calculateDamage(opponent, weapon); opponent.setHp(opponent.getHp() - damage); Utility.updatePlayer(opponent); updateState(); } public State getState() { return state; } ... } .
  11. public class Fight { private State state = new State();

    private Player player1, player2; public Fight(Player player1, Player player2) { this.player1 = player1; this.player2 = player2; } ... public void attack(Player attacker, Weapon weapon) { Player opponent = getOpponentOf(attacker); int damage = calculateDamage(opponent, weapon); opponent.setHp(opponent.getHp() - damage); Utility.updatePlayer(opponent); updateState(); } public State getState() { return state; } ... } .
  12. 2 .

  13. )  )) ) ( )    

  14. ()      <<interface>>   

    
  15. F . interface PlayerUpdater { public void updatePlayer(Player player); }

    F
  16. public class PlayerUpdaterImpl implements PlayerUpdater { @Override public void updatePlayer(Player

    player) { Utility.updatePlayer(player); } } .
  17. B . public class Fight { private State state =

    new State(); private Player player1, player2; private final PlayerUpdater updater; public Fight(Player player1, Player player2) { this(player1, player2, new PlayerUpdaterImpl()); } protected Fight(Player player1, Player player2, PlayerUpdater updater) { this.player1 = player1; this.player2 = player2; this.updater = updater; } ... public void attack(Player attacker, Weapon weapon) { Player opponent = getOpponentOf(attacker); int damage = calculateDamage(opponent, weapon); opponent.setHealth(opponent.getHealth() - damage); updater.updatePlayer(opponent); updateState(); } } . P U . . .. . F
  18. P . ( ( public class Fight { private State

    state = new State(); private Player player1, player2; private final PlayerUpdater updater; public Fight(Player player1, Player player2) { this(player1, player2, new PlayerUpdaterImpl()); } protected Fight(Player player1, Player player2, PlayerUpdater updater) { this.player1 = player1; this.player2 = player2; this.updater = updater; } ... public void attack(Player attacker, Weapon weapon) { Player opponent = getOpponentOf(attacker); int damage = calculateDamage(opponent, weapon); opponent.setHealth(opponent.getHealth() - damage); updater.updatePlayer(opponent); updateState(); } } ) ) . I U B D ( I ( ( I F
  19. import static org.mockito.Mockito.*; public class FightTest { @Test public void

    testAttack() { Player player1 = new Player(); Player player2 = new Player(); Weapon weapon = new Weapon(); // PlayerUpdater    PlayerUpdater updater = mock(PlayerUpdater.class); // Fight   Fight fight = new Fight(player1, player2, updater); fight.registerHit(player1, weapon); fight.registerHit(player1, weapon); fight.registerHit(player1, weapon); State state = fight.getState(); assertThat(state.isFinished(), is(true)); // updatePlayer() 3   verify(updater, times(3)).updatePlayer(player2); } } M . F P U F
  20. I • I • • I B • D J

    B D J • • B D J • • • A
  21. I • • ( • g • JgW P •

    O D • g • • ( • e • ( Vc ) • / l g A • aj Aa g • j • u B t
  22. yw • yw fo a • 2101 • yw fmea

    • p wcD m p D w • c • I m p w • . • 385 2/ • • t mD n p wc p w c cp • W myw c dmea • JP n wn Or p • :AA B8A 78 4 8 A # # # 9
  23. • • )( ( • PJ • B H P

    • HAD • PB • • - ) • O • H • S H I
  24. • O B • • B • P D AP

    • D • P • • • J • . e e • D B J • Wb
  25. .

  26. • ) • ) • ( ) • • •

    C I • • •
  27. • ( ) - • ( ( (

  28. 

  29. c • n o tp c a J ejr E

    e e • n o tp e D e • n o tp I jr f • ) ) () 3 ) a t • ) ) ( ( d f
  30. C B • c e E J • M a

    FJ • d J • 3 J
  31. 1. • H UaS u • f I gJ jP

    • L • 7 _ - - • vR WE c T WE T c D? •