1 / 19

JUnit & Refactoring

JUnit & Refactoring. 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. Why using unit-tests ?.

kirti
Download Presentation

JUnit & Refactoring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JUnit & Refactoring

  2. JUnit • Unittestingframework • What to test • the behaviour of a object • the behaviour of onepart of a object • the behaviour of the interactionbetweenobjects

  3. Whyusingunit-tests? "Unit Testing gives you more bang for your buck” (MiskoHevery – Agile Coach @ Google)

  4. A goodtestshouldbe • Accurate • Independent • Reusable • Easy to run • Fast to run

  5. A.A.A phases • Arrange: prepare the test data • Act: do what you are testing • Assert: check the results • assertin Junit

  6. A Java TestCase • is a methodannotated with @Test • orwith@Test(expected=Exception) iftesting for exceptions • has a signaturepublicvoidtestName() • hasfixturesareprovidedby@Beforeand @BeforeClassannotations • hastear-downsareprovidedby@Afterand @AfterClassannotations • canbegroupedusing a Suite

  7. JUnitasserts • assertArrayEquals • assertEquals • assertFalse • assertNotNull • assertNotSame • assertNull • assertSame • assertTrue

  8. When stop unittesting? • Testeverything… thatcouldpossiblybreak! • When you can provide better value by doing something else.

  9. Example • A ShoppingCart in which the usercan: • additems • removeitems • get the totalnumber of items in the cart • get the totalcost of the items in the cart • handleexception(s) Need help? http://www.junit.org/junit/javadoc/4.3/

  10. Refactoring • Changes to the codethatmakeitmorereadablebutkeep the sameexternalbehaviour • Fixes to codesmells • Relies on tests

  11. Consolidateconditionalexpression • Multiple conditionals can be extracted into method(s) • Theyhave to beindependent

  12. Extractclass/method • When a classor a methodaretoo ”fat” • Remove a part of it to makeitsownclassormethod • Areusuallyworking on the samekind of data or intention

  13. Extractinterface • A subset of a class is usedbyseveralothers • Extract the subset in a interface • The classimplements the new interface

  14. Introduceparameterobject • A number of parametersarepassedtogetherseveraltime • Replacethem with an object

  15. Preservewholeobject • A number of parametersfrom an objectarethenpassed to a method • Pass the wholeobject to the methodcall

  16. Renamemethod/”magicnumber” • Method namesshouldcommunicatetheirtask • Ifyoucannotproperlyname a methoditmightbe ”fat” • Hardcodevariablesshouldbedeclaredconstant

  17. Replaceparameter with method • A method is invokedby and object and the resultpassed to anothermethod • Let the receiverinvoke the method

  18. Resources • Kent Beck, JUnitTestInfected • Martin Fowler, Refactoring: Improving the Design of Existing Code • www.junit.com • www.eclipse.org • www.refactoring.com

  19. 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

More Related