190 likes | 285 Views
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 ?.
E N D
JUnit • Unittestingframework • What to test • the behaviour of a object • the behaviour of onepart of a object • the behaviour of the interactionbetweenobjects
Whyusingunit-tests? "Unit Testing gives you more bang for your buck” (MiskoHevery – Agile Coach @ Google)
A goodtestshouldbe • Accurate • Independent • Reusable • Easy to run • Fast to run
A.A.A phases • Arrange: prepare the test data • Act: do what you are testing • Assert: check the results • assertin Junit
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
JUnitasserts • assertArrayEquals • assertEquals • assertFalse • assertNotNull • assertNotSame • assertNull • assertSame • assertTrue
When stop unittesting? • Testeverything… thatcouldpossiblybreak! • When you can provide better value by doing something else.
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/
Refactoring • Changes to the codethatmakeitmorereadablebutkeep the sameexternalbehaviour • Fixes to codesmells • Relies on tests
Consolidateconditionalexpression • Multiple conditionals can be extracted into method(s) • Theyhave to beindependent
Extractclass/method • When a classor a methodaretoo ”fat” • Remove a part of it to makeitsownclassormethod • Areusuallyworking on the samekind of data or intention
Extractinterface • A subset of a class is usedbyseveralothers • Extract the subset in a interface • The classimplements the new interface
Introduceparameterobject • A number of parametersarepassedtogetherseveraltime • Replacethem with an object
Preservewholeobject • A number of parametersfrom an objectarethenpassed to a method • Pass the wholeobject to the methodcall
Renamemethod/”magicnumber” • Method namesshouldcommunicatetheirtask • Ifyoucannotproperlyname a methoditmightbe ”fat” • Hardcodevariablesshouldbedeclaredconstant
Replaceparameter with method • A method is invokedby and object and the resultpassed to anothermethod • Let the receiverinvoke the method
Resources • Kent Beck, JUnitTestInfected • Martin Fowler, Refactoring: Improving the Design of Existing Code • www.junit.com • www.eclipse.org • www.refactoring.com
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