Unit Testing 
By Mohamad Asmar a.k.a DaiGooR
Me! 
Mohamed AsmerDeveloper 
daigoor@gmail.com 
www.daigoor.com@daigoor 
(+970) 598 917 280
Developer life ! 
the more pressure you feel, the fewer tests you writes. The fewer tests you write, the less productive you are and the less stable your code becomes. The less productive and accurate you are, the more pressure you feel.
Def. 
Wikipedia :unit testingis asoftware testingmethod by which individual units ofsource code 
In a simple way … validate each and every unit of the software perform as designed.
Def. Cont. 
A Unit is the smallest part of code 
›{function or procedure} 
›The smallest part that can be compiled by it self.
Ex 
public int stringToInt( String str ){ return Integer.parseInt(str); } 
stringToInt(null); 
stringToInt(“TEXT”) 
stringToInt(“1234567890987654321”)
Why ? 
Without it defects will appear at the end of the cycle! 
Trace bugs is time consuming , hard and complex 
Check new feature if they are feasible 
Check input and output values 
Correct outputs fast
Why ? -_- 
Reduce future cost ! 
Faster development 
Better design 
Faster debugging 
Excellent regression tool + 
it provide sort of documentation
Categories 
White-Box : function exe and checked 
Black-Box : input/output & app-interface
Types 
Positive test (end user testing) 
›Valid parameter 
Negative test 
›Not valid parameter
Ex 
public int stringToInt( String str ){ return Integer.parseInt(str); } 
stringToInt(null)//N 
stringToInt(“TEXT”)//N 
stringToInt(“1234567890987654321”)//N 
stringToInt(“1234”)//P
When to write the test? 
“Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.”...
story 
:$
Traditional Testing Strategies 
Print Statements 
Use of Debugger 
Debugger Expressions
Ex. 
... List<?> getVersionsForProductId(Long id){ … return List<?> } 
The Tests That AreNeeded 
Startingfromtheeasiesttothehardest: 
Ifthere’snoproductforthatid,thenanexceptionisthrown. 
Ifthere’snoversionsforavalidproductid,thenanemptylistisreturned. 
Ifthereareversionsforaproductid,thenanon-emptylistofalltheversionsisreturned.
Live Show 
Eclipse 
IntelliJ Idea
Keep Unit tests small and fast We should run them after each checkin 
keep them UN-dependent They should not depend on each other 
They should be simple 
Fix error immediately
Its all about unit There should be a test class per each class .. no more no less ! You should not right an application to test another application. 
Start with the simple test 
Code convention

Unit testing

  • 1.
    Unit Testing ByMohamad Asmar a.k.a DaiGooR
  • 2.
    Me! Mohamed AsmerDeveloper daigoor@gmail.com www.daigoor.com@daigoor (+970) 598 917 280
  • 3.
    Developer life ! the more pressure you feel, the fewer tests you writes. The fewer tests you write, the less productive you are and the less stable your code becomes. The less productive and accurate you are, the more pressure you feel.
  • 4.
    Def. Wikipedia :unittestingis asoftware testingmethod by which individual units ofsource code In a simple way … validate each and every unit of the software perform as designed.
  • 5.
    Def. Cont. AUnit is the smallest part of code ›{function or procedure} ›The smallest part that can be compiled by it self.
  • 6.
    Ex public intstringToInt( String str ){ return Integer.parseInt(str); } stringToInt(null); stringToInt(“TEXT”) stringToInt(“1234567890987654321”)
  • 7.
    Why ? Withoutit defects will appear at the end of the cycle! Trace bugs is time consuming , hard and complex Check new feature if they are feasible Check input and output values Correct outputs fast
  • 8.
    Why ? -_- Reduce future cost ! Faster development Better design Faster debugging Excellent regression tool + it provide sort of documentation
  • 10.
    Categories White-Box :function exe and checked Black-Box : input/output & app-interface
  • 11.
    Types Positive test(end user testing) ›Valid parameter Negative test ›Not valid parameter
  • 12.
    Ex public intstringToInt( String str ){ return Integer.parseInt(str); } stringToInt(null)//N stringToInt(“TEXT”)//N stringToInt(“1234567890987654321”)//N stringToInt(“1234”)//P
  • 13.
    When to writethe test? “Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.”...
  • 14.
  • 15.
    Traditional Testing Strategies Print Statements Use of Debugger Debugger Expressions
  • 16.
    Ex. ... List<?>getVersionsForProductId(Long id){ … return List<?> } The Tests That AreNeeded Startingfromtheeasiesttothehardest: Ifthere’snoproductforthatid,thenanexceptionisthrown. Ifthere’snoversionsforavalidproductid,thenanemptylistisreturned. Ifthereareversionsforaproductid,thenanon-emptylistofalltheversionsisreturned.
  • 17.
    Live Show Eclipse IntelliJ Idea
  • 18.
    Keep Unit testssmall and fast We should run them after each checkin keep them UN-dependent They should not depend on each other They should be simple Fix error immediately
  • 19.
    Its all aboutunit There should be a test class per each class .. no more no less ! You should not right an application to test another application. Start with the simple test Code convention