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

Unit Testing and sleep well

Karumi
June 21, 2016

Unit Testing and sleep well

Why do we need write unit test? What is a unit test? What is a test double? Why kind of test doubles exist? A light and first look to unit testing.

Karumi

June 21, 2016
Tweet

More Decks by Karumi

Other Decks in Technology

Transcript

  1. Unit Testing and sleep well Jorge Juan Barroso Carmona [email protected]

    @flipper83 +JorgeJBarroso Android Expert Android GDE
  2. Adam Tornhill “Testing is the process of executing a program

    with the intent of finding errors. ... This definition of testing has many implications ... it implies that testing is a destructive process, even a sadistic process, which explains why most people find it difficult.” Glenford J. Myers
  3. public class MultiplyTest {
 @Test
 public void shouldReturnValidResultWhenMultiplyTwoValues(){ 
 Multiply

    multiply = new Multiply(new Addition());
 int multiplyValue = multiply.multiply(2, 3);
 assertEquals(6, multiplyValue); 
 }
 }
  4. public Calculator(OutputWriter outputWriter, InputReader inputReader, Memory memory) {
 this.outputWriter =

    outputWriter;
 this.inputReader = inputReader;
 this.memory = memory;
 this.addition = new Addition();
 this.multiply = new Multiply(addition);
 }
  5. @Test
 public void shouldShowAdditionResultOnTheOutputStreamWhenCallAdition() throws Exception {
 MockOutputWriter outputWriter =

    givenOutputWriter();
 InputReader inputReader = givenInputReader(2, 3);
 Memory memory = givenDummyMemory();
 Calculator calculator = givenCalculator(outputWriter, inputReader, memory);
 
 calculator.insertValue();
 calculator.insertValue(); calculator.add();
 
 assertEquals(3, outputWriter.getNumCalls());
 assertEquals(5, outputWriter.getArgumentCatcher());
 }
  6. Dummy Not make anything private Memory givenDummyMemory() {
 return new

    DummyMemory();
 } public class DummyMemory implements Memory {
 @Override
 public void put(int value, int position) {
 }
 
 @Override
 public int get(int memoryPosition) {
 return 0;
 }
 }
  7. Stubs Canned responses public class StubInputReader implements InputReader {
 private

    final int[] values;
 private int count = 0;
 
 public StubInputReader(int... values) {
 this.values = values;
 }
 
 @Override
 public int read() {
 int response = values[count];
 count = (count + 1) % values.length;
 return response;
 }
  8. Mocks Expectations public class MockOutputWriter implements OutputWriter {
 private int

    numCalls;
 private int argument;
 
 @Override
 public void write(int value) {
 numCalls ++;
 argument = value;
 }
 public int getNumCalls() {return numCalls;}
 public int getArgumentCatcher() {return argument;} }

  9. Spies Save information public void shouldStoreInMemoryLastOperationResult(){
 … SpyMemory memory =

    givenSpyMemory(); …
 
 calculator.insertValue();
 calculator.insertValue();
 calculator.add();
 calculator.save(0);
 calculator.insertValue();
 calculator.insertValue();
 calculator.save(1);
 
 List<Integer> values = memory.getValues();
 assertEquals(5, values.get(0).intValue());
 assertEquals(5, values.get(1).intValue());

  10. Spies Save information public class SpyMemory implements Memory {
 List<Integer>

    values = new ArrayList<Integer>();
 @Override
 public void put(int value, int position) {
 values.add(value);
 }
 @Override
 public int get(int memoryPosition) { return 0;}
 
 public List<Integer> getValues() {
 return values;
 }
 }
  11. Fake Near to real one shouldShowAdditionResultWhenCallAditionWithSavedValueOnMemory(){
 … Memory memory =

    givenFakeMemory(); …
 
 calculator.insertValue();
 calculator.insertValue();
 calculator.add();
 calculator.save(0);
 calculator.insertValue();
 calculator.insertValueFromMemory(0);
 calculator.add();
 
 assertEquals(7, outputWriter.getArgumentCatcher());
 }
  12. Fake Near to real one public class FakeMemory implements Memory

    {
 Map<Integer, Integer> mem = new HashMap<Integer, Integer>();
 
 @Override
 public void put(int value, int position) {
 mem.put(position, value);
 }
 
 @Override
 public int get(int memoryPosition) {
 return mem.get(memoryPosition);
 }
 }
  13. Adam Tornhill “When you are very thirsty, even dirty water

    will keep you alive.” J. B. Rainsberger
  14. TDD

  15. Bibliography They’re the cracks! Adicto Al verde. Joaquin Engelmo Elegant??

    Unit Testing. Pablo Guardiola Working Effectively with Unit testing. Jay Fields Software Craftmanship. Sandro Mancuso Mocks Aren't Stubs. martin fowler. 2007 Software Reliability: Principles & Practices. Glenford J. Myers 1976 The Art of Unit Testing. Roy Osherove Test Driven Development: By Example. Kent Beck