Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to write testable code

How to write testable code

perforb

June 04, 2018
Tweet

More Decks by perforb

Other Decks in Programming

Transcript

  1. .

  2. . 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; } ... }
  3. . 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)); } }
  4. 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; } ... } .
  5. 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; } ... } .
  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. 2 .

  8. 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
  9. 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
  10. 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
  11. I • I • • I B • D J

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

    O D • g • • ( • e • ( Vc ) • / l g A • aj Aa g • j • u B t
  13. 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
  14. • • )( ( • PJ • B H P

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

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

  17. 

  18. 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
  19. C B • c e E J • M a

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

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