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

Unit Testing

Unit Testing

A talk on the basics of unit testing.

Discussion
http://goo.gl/NdpQ4

Markos Charatzas

February 15, 2013
Tweet

More Decks by Markos Charatzas

Other Decks in Programming

Transcript

  1. Know what you are testing 90%1 of tests fail to

    show intent 1. not scientific
  2. AdditionTest public class AdditionTest { @Test public void testGivenTwoNumbersAssertTotal() {

    //Preconditions Addition addition = new Addition(); int x = 1; int y = 1; //Execution int actual = addition.add(x, y); //Assertions assertEquals(2, actual); } }
  3. Algorithmic Do what you say1 1. as part of your

    public contract (Documentation)
  4. CalculatorTest public class CalculatorTest { @Test public void testGivenSubstractionAssertMake() {

    Calculator calculator = new Calculator(); /* * Cannot test in isolation * Added noise, is the input relevant? * Is the test actually valid? */ Subtraction subtraction = new Subtraction(2,2); int actual = calculator.make(subtraction); assertEquals(0, actual); } }
  5. Use mocks public class CalculatorTest { @Test public void testGivenSubstractionAssertMake()

    { Calculator calculator = new Calculator(); Subtraction subtractionMock = createMock(Subtraction.class); subtractionMock.make(); replay(subtractionMock); calculator.make(subtractionMock); verify(subtractionMock); } }
  6. CoffeeMakerTest public class CoffeeMakerTest { @Test public void testSteam() {

    /* * CoffeeGrinder adds noise * Not testing in isolation */ CoffeeMaker coffeeMaker = new CoffeeMaker( new CoffeeGrinder(), new WaterTank()); coffeeMaker.steam(); } }
  7. Embrace null public class CoffeeMakerTest { @Test public void testGivenWaterTankAssertSteam()

    { WaterTank waterTankMock = createMock(WaterTank.class); waterTankMock.heat(); replay(waterTankMock); CoffeeMaker coffeeMaker = new CoffeeMaker( null, waterTankMock); coffeeMaker.steam(); verify(waterTankMock); } }
  8. ArrayTest public class ArrayTest { @Test public void testGivenTwoNonEmptyArraysAssertNotEqual() {

    /* * Don't use values specific to your app. * They add noise. Make the test fragile. * Use random variable names to show intent. * Any two arrays in this case. */ Array<String> one = Array.of( new String[]{"foo"} ); Array<String> another = Array.of( new String[]{"bar"} ); assertThat(one, not(equalTo(another))); } }
  9. RandomTest public class RandomTest { @Test public void testCurrentTimeInMillis() throws

    Exception { long now = System.currentTimeMillis(); //Time dependent Thread.sleep(1); Assert.assertEquals(System.currentTimeMillis(), now); } }
  10. GlobalTest public class GlobalTest { private CoffeeMaker newCoffeeMaker; @Before public

    void before() { this.newCoffeeMaker = CoffeeMaker.newCoffeeMaker(); } @After public void after() { this.newCoffeeMaker.cleanup(); } @Test public void testCoffeeMakerGrindsCoffee() { newCoffeeMaker.grind(); } @Test public void testCoffeeMakerSteamsWater() { newCoffeeMaker.steam(); } }
  11. LocalTest public class LocalTest { @Test public void testCoffeeMakerGrindsCoffee() throws

    Exception { /* * Clear preconditions * Self contained * Defined contract for CoffeeMaker */ CoffeeMaker newCoffeeMaker = CoffeeMaker.newCoffeeMaker(); newCoffeeMaker.grind(); } @Test public void testCoffeeMakerSteamsWater() throws Exception { CoffeeMaker newCoffeeMaker = CoffeeMaker.newCoffeeMaker(); newCoffeeMaker.steam(); } }
  12. AsyncTest public class AsyncTest { @Test public void testGivenTwoSecondsAssertDidRun() throws

    Exception { final CountDownLatch singleCount = new CountDownLatch(1); //Equal to the number of threads final AtomicBoolean didRun = new AtomicBoolean(); //Be safe1 Executors.callable(new Runnable() { public void run() { didRun.set(true); singleCount.countDown(); //Call at the end of execution } }).call(); singleCount.await(2, TimeUnit.SECONDS); //Don't want infinite blocking tests Assert.assertTrue(didRun.get()); } } 1. generally