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

JUnit and Refactorings

JUnit and Refactorings

Davide Fucci

June 19, 2014
Tweet

More Decks by Davide Fucci

Other Decks in Programming

Transcript

  1. JUnit § Unit testing framework § What to test § the behaviour of

    a object § the behaviour of one part of a object § the behaviour of the interaction between objects
  2. Why using unit-tests? "Unit Testing gives you more bang for

    your buck” (Misko Hevery – Agile Coach @ Google)
  3. A.A.A phases § Arrange: prepare the test data § Act: do what

    you are testing § Assert: check the results §  assert in Junit
  4. A Java Test Case § is a method annotated with @Test

    § or with @Test(expected=Exception) if testing for exceptions § has a signature public void testName() § has fixtures are provided by @Before and @BeforeClass annotations § has tear-downs are provided by @After and @AfterClass annotations § can be grouped using a Suite
  5. When stop unit testing? § Test everything… that could possibly break!

    § When you can provide better value by doing something else.
  6. Example § A Shopping Cart in which the user can: § add

    items § remove items § get the total number of items in the cart § get the total cost of the items in the cart § handle exception(s) Need help? http://www.junit.org/junit/javadoc/4.3/
  7. Refactoring § Changes to the code that make it more readable

    but keep the same external behaviour § Fixes to code smells § Relies on tests
  8. Extract class/method § When a class or a method are too

    ”fat” § Remove a part of it to make its own class or method § Are usually working on the same kind of data or intention
  9. Extract interface § A subset of a class is used by

    several others § Extract the subset in a interface § The class implements the new interface
  10. Preserve whole object § A number of parameters from an object

    are then passed to a method § Pass the whole object to the method call
  11. Rename method/”magic number” § Method names should communicate their task § If

    you cannot properly name a method it might be ”fat” § Hardcode variables should be declared constant
  12. Replace parameter with method § A method is invoked by and

    object and the result passed to another method § Let the receiver invoke the method
  13. Resources § Kent Beck, JUnit Test Infected § Martin Fowler, Refactoring: Improving

    the Design of Existing Code § www.junit.com § www.eclipse.org § www.refactoring.com
  14. Exercise The Romans were a clever bunch. They conquered most

    of Europe and ruled it for hundreds of years. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. The Romans wrote numbers using letters - I, V, X, L, C, D, M. Write a function to convert from normal to Roman Numerals: 1 --> I 10 --> X 7 --> VII and so on… Note that you can't write numerals like "IM" for 999. Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. To see this in practice, consider the ... example of 1990. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. Acceptance test: MMDCCLI à 2751 Bonus: Write a function to convert from Roman to normal numbers