Share
Embed
Transcript
19 slides
-
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? "Unit Testing gives you more bang for
your buck” (Misko Hevery – Agile Coach @ Google) -
A good test should be § 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 § assert in Junit -
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 -
When stop unit testing? § Test everything… that could possibly break!
§ When you can provide better value by doing something else. -
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/ -
Refactoring § Changes to the code that make it more readable
but keep the same external behaviour § Fixes to code smells § Relies on tests -
Consolidate conditional expression § Multiple conditionals can be extracted into method(s)
§ They have to be independent -
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 -
Extract interface § A subset of a class is used by
several others § Extract the subset in a interface § The class implements the new interface -
Introduce parameter object § A number of parameters are passed together
several time § Replace them with an object -
Preserve whole object § A number of parameters from an object
are then passed to a method § Pass the whole object to the method call -
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 -
Replace parameter with method § A method is invoked by and
object and the result passed to another method § Let the receiver invoke the method -
Resources § Kent Beck, JUnit Test Infected § 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