SlideShare una empresa de Scribd logo
1 de 73
  Unit  Testing  with JUnit Alessandro Marchetto Fondazione Bruno Kessler - IRST
Iterative Software development + system increment Prioritized  functionalities Write acceptance tests Execute acceptance tests Write and execute unit tests “ Executed after the development ” “ Written before ”
Business Logic GUI Web UI Persistence  Layer Jemmy/Abbot/JFCUnit/… HttpUnit/Canoo/Selenium Junit/SQLUnit/XMLUnit FIT/Fitnesse ( High level ) Junit ( Low level ) Cactus Perfomance and  Load Testing JMeter/JUnitPerf Testing tools
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Testing with JUnit
Eclipse ,[object Object],[object Object],[object Object],[object Object],[object Object],IDE = “Integrated development environment” ,[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],Junit (3.x and 4.x)  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
JUnit 3.x for testing programs ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Framework elements ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Testsuite Testcase 2 Testcase 1 Testcase 3
TestCase Class: an example   ,[object Object],[object Object],[object Object],[object Object],[object Object],Must begin with “test”
Assert*() ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Method family to check conditions …
Assert*() ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],obtained http://junit.org/apidocs/org/junit/Assert.html
Assert: example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],class Stack { public boolean isEmpty(){ ...  } public void push(int i){ ...  } public int pop(int i){ ...  } … }
One concept at a time … ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Code Modularization …
Working rule ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
TestSuite ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],junit.framework.*
Test of “Exceptions” ,[object Object],[object Object],[object Object]
We expect a normal behavior … ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],class TheClass { public void method(String p) throws PossibleExcetion { /*... */ } }
We expect an exception  … try { //  we call the method with wrong parameters object.method( null ); fail (“method should fail!!"); } catch(PossibleException e){  assertTrue(true); // OK } class TheClass { public void method(String p) throws PossibleException { /*... */ } }
SetUp() and tearDown() ,[object Object],[object Object],[object Object],[object Object],ShoppingCart cart; Book book; protected void  setUp()  { cart = new ShoppingCart(); book = new Book(“JUnit", 29.95); cart.addItem(book); }  …
Junit in eclipse - Setup ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Create a new JUnit test case  ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Eclipse Menu File  Edit Source Refactor Navigate Search Project Run Window Help
Run as JUnit Test ,[object Object],[object Object],[object Object],Eclipse Menu File Edit Source Refactor Navigate Search Project  Run  Window Help
Red / Green Bar Fail Pass expected <-3> but was <-4>
JUnit 3.x and JUnit 4.x ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
From JUnit 3.x to 4.x ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],Annotations in J2SE
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Annotations in J2SE … an example
Junit 4.x for testing programs ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],Junit 4.x for testing programs (2)
[object Object],[object Object],[object Object],[object Object],Junit 4.x for testing programs (3)
@Before and @After methods ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],Junit 4.x for testing programs (4)
Additional Features of @Test ,[object Object],[object Object],[object Object],[object Object]
Parameterized tests ,[object Object],[object Object],import  org.junit.runner.RunWith; import  org.junit.runners.Parameterized; import  org.junit.runners.Parameterized.Parameters; Parameters used to exercise  different instances of the class
Test suites ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],import  org.junit.runners.Suite ; import  org.junit.runners.Suite.SuiteClasses ; It could be empty
Additional features of Junit 4 ,[object Object],[object Object],[object Object],@Override public   boolean  equals(Object o){ …  return   … ; }
Autoboxing example ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
public class  MySum  {   public static int  sum (int a, int b)  { return a+b;  }  }  Summarizing… an example (Junit 3.x) Test case 1 import  junit.framework.TestCase; import  math.Add; public   class  TestCase1_add_Junit3  extends  TestCase { public   void  testAdd() { Add add= new  Add(); int  sum=add. sum (3, 2); assertEquals (5, sum); } }
Summarizing… an example (Junit 3.x) Test case 2 import  junit.framework.TestCase; import  math.Add; public   class  TestCase1_add_Junit3  extends  TestCase { public   void  testAdd() {…  } public   void  testAdd_2() { Add add= new  Add(); int  sum=add. sum (3, -2); assertEquals (5, sum); } }
Summarizing… an example (Junit 3.x) Test case 2 import  junit.framework.TestCase; import  math.Add; public   class  TestCase1_add_Junit3  extends  TestCase { public   void  testAdd() {…  } public   void  testAdd_2() { Add add= new  Add(); int  sum=add. sum (3, -2); assertEquals (5, sum); } }
public class  MySum  {   public static int  sum (int a, int b)  { return a+b;  }  }  Summarizing… an example (Junit 4.x) import  math.Add; import  org.junit.*; import   static  org.junit.Assert.*; public   class  Testcase1_add_Junit4 { @Test public   void  testAdd() { Add add= new  Add(); int  sum=add. sum (3, 2); assertEquals (5, sum); } }
When testing programs? ,[object Object],[object Object],[object Object],[object Object]
Test last New  functionality Understand Implement functionality Write tests  Run all tests Result? Rework fail pass Next functionality
Test first “ Extreme programming”  ( XP ) champions the use of tests as a development tool … New functionality Understand Add a single test Add code for the test Run all test Result? Rework Functionality complete? fail pass No Next functionality Yes “ Extreme programming”  ( XP ) champions the use of tests as a development tool …
Test First Advantages ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Add the skeleton of the class and methods (without body) Test-first with Junit Rework Refactoring “ improving the structure” Add a testcase Run test Run test
Junit in practice  … ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Add a testcase
Add Testcases for the  settlement  method  class  Test_CurrentAccount  extends TestCase { public void  test_settlementVoid () { CurrentAccount c = new CurrentAccount(); assertEquals (0, c.settlement()); } public void  test_settlement () { CurrentAccount c = new CurrentAccount(); c.deposit(12); c.draw(-8); c.deposit(10); assertEquals (14, c.settlement()); } } test first …
Add the skeleton of the class and methods (without body)
Add the skeletone code of the method class  CurrentAccount  { int account[]; int lastMove; CurrentAccount (){ lastMove=0;  account=new int[10]; } public void  deposit (int value){ account[lastMove]=value;  lastMove++; } public void  draw (int value) { account[lastMove]=value;  lastMove++; } public int  settlement () {return 0;} public static void  main (String args[]) {} } class  Test_CurrentAccount  extends TestCase { public void  test_settlementVoid () { currentAccount c = new currentAccount(); assertEquals (0, c.settlement()); } public void  test_settlement () { currentAccount c = new currentAccount(); c.deposit(12); c.draw(-8); c.deposit(10); assertEquals (14, c.settlement()); } }
Run test
Run Junit ( first time )
Rework
Rework class  CurrentAccount  { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[10]; } public void  deposit (int value){ …} public void  draw (int value) { …} public int  settlement ()  { int result = 0 for (int i=0; i<account.length; i++) { result = result + account[i]; } return result; } public static void  main (String args[]) {} } class  Test_CurrentAccount  extends TestCase { public void  test_settlementVoid () { currentAccount c = new currentAccount(); assertEquals (0, c.settlement()); } public void  test_settlement () { currentAccount c = new currentAccount(); c.deposit(12); c.draw(-8); c.deposit(10); assertEquals (14, c.settlement()); } }
Run test
Run Junit ( second time )
Add a testcase
Add a new testcase class  CurrentAccount  { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[10]; } public void  deposit (int value){ …} public void  draw (int value) { …} public int  settlement ()  { int result = 0 for (int i=0; i<account.length; i++) { result = result + account[i]; } return result; } public static void  main (String args[]) {} } class  Test_currentAccount  extends TestCase { … public void  test_realCaseSettlement () { currentAccount c = new currentAccount(); for (int i=0; i <10 ; i++) c.deposit(1); c.draw(-10); assertEquals (0, c.settlement()); } }
Run test
Run JUnit ( third time ) Run time error
Rework
class  CurrentAccount  { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[ 100 ]; } public void  deposit (int value){ …} public void  draw (int value) { …} public int  settlement ()  { int result = 0 for (int i=0; i<account.length; i++) { result = result + account[i]; } return result; } public static void  main (String args[]) {} } class  Test_currentAccount  extends TestCase  { … public void  test_realCaseSettlement () { currentAccount c = new currentAccount(); for (int i=0; i <10 ; i++) c.deposit(1); c.draw(-10); assertTrue (0, c.settlement()); } } Rework
Run test
Run JUnit ( fourth time )
Refactoring “ improving the structure”
Refactoring “ changing the data structure: Array --> List” public class  CurrentAccount  { List account = new LinkedList(); public void  deposit (int value) { account.add(new Integer(value)); } public void  draw (int value) { account.add(new Integer(value)); } public int  settlement () { int result = 0; Iterator it=account.iterator(); while (it.hasNext()) { Integer value_integer = (Integer)it.next(); int val = value_integer.intValue(); result = result + val; } return result; } }
Run test
Run JUnit ( fifth time )
The End
xUnit ,[object Object],[object Object],[object Object],[object Object],2.21  Perl 2.22 PHP   2.23 PL/SQL   2.24  PowerBuilder   2.25  Prolog   2.26 Python   2.27 REALbasic   2.28 Ruby   2.29 SAS   2.30 Scala   2.31 Shell   2.32 Simulink   2.33 Smalltalk   2.34 SQL   2.35 Tcl   2.36 Transact-SQL   2.37 Visual FoxPro   2.38 Visual Basic   2.39 XML   2.40 XSLT   2.41  Other   2.1  ActionScript   2.2 Ada   2.3 BPEL   2.4 C   2.5 C++   2.6  ColdFusion  (CFML)   2.7 Delphi   2.8 Emacs Lisp   2.9 Fortran   2.10 Haskell   2.11 Internet   2.12 Java   2.13 JavaScript   2.14 Lasso   2.15 MATLAB   2.16 MySQL   2.17 .NET programming languages   2.18 Objective-C   2.19 Ocaml   2.20 PegaRULES  Process   Commander
“ The tip of the iceberg”   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
References ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
Mathieu Carbou
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
nickokiss
 

La actualidad más candente (20)

Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
JUnit 4
JUnit 4JUnit 4
JUnit 4
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Testing with Junit4
Testing with Junit4Testing with Junit4
Testing with Junit4
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
JUnit & Mockito, first steps
JUnit & Mockito, first stepsJUnit & Mockito, first steps
JUnit & Mockito, first steps
 
Junit
JunitJunit
Junit
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 
Thread & concurrancy
Thread & concurrancyThread & concurrancy
Thread & concurrancy
 
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool TimeJUnit 5 - The Next Generation of JUnit - Ted's Tool Time
JUnit 5 - The Next Generation of JUnit - Ted's Tool Time
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
J Unit
J UnitJ Unit
J Unit
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Junit
JunitJunit
Junit
 
Software testing basics and its types
Software testing basics and its typesSoftware testing basics and its types
Software testing basics and its types
 

Similar a 3 j unit

J unit presentation
J unit presentationJ unit presentation
J unit presentation
Priya Sharma
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
Yi-Huan Chan
 

Similar a 3 j unit (20)

Junit With Eclipse
Junit With EclipseJunit With Eclipse
Junit With Eclipse
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
Pragmatic unittestingwithj unit
Pragmatic unittestingwithj unitPragmatic unittestingwithj unit
Pragmatic unittestingwithj unit
 
8-testing.pptx
8-testing.pptx8-testing.pptx
8-testing.pptx
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Junit_.pptx
Junit_.pptxJunit_.pptx
Junit_.pptx
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
Java custom annotations example
Java custom annotations exampleJava custom annotations example
Java custom annotations example
 
Test ng tutorial
Test ng tutorialTest ng tutorial
Test ng tutorial
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
JUnit
JUnitJUnit
JUnit
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Unit testing
Unit testingUnit testing
Unit testing
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 

3 j unit

  • 1. Unit Testing with JUnit Alessandro Marchetto Fondazione Bruno Kessler - IRST
  • 2. Iterative Software development + system increment Prioritized functionalities Write acceptance tests Execute acceptance tests Write and execute unit tests “ Executed after the development ” “ Written before ”
  • 3. Business Logic GUI Web UI Persistence Layer Jemmy/Abbot/JFCUnit/… HttpUnit/Canoo/Selenium Junit/SQLUnit/XMLUnit FIT/Fitnesse ( High level ) Junit ( Low level ) Cactus Perfomance and Load Testing JMeter/JUnitPerf Testing tools
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18. We expect an exception … try { // we call the method with wrong parameters object.method( null ); fail (“method should fail!!&quot;); } catch(PossibleException e){ assertTrue(true); // OK } class TheClass { public void method(String p) throws PossibleException { /*... */ } }
  • 19.
  • 20.
  • 21.
  • 22.
  • 23. Red / Green Bar Fail Pass expected <-3> but was <-4>
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38. public class MySum { public static int sum (int a, int b) { return a+b; } } Summarizing… an example (Junit 3.x) Test case 1 import junit.framework.TestCase; import math.Add; public class TestCase1_add_Junit3 extends TestCase { public void testAdd() { Add add= new Add(); int sum=add. sum (3, 2); assertEquals (5, sum); } }
  • 39. Summarizing… an example (Junit 3.x) Test case 2 import junit.framework.TestCase; import math.Add; public class TestCase1_add_Junit3 extends TestCase { public void testAdd() {… } public void testAdd_2() { Add add= new Add(); int sum=add. sum (3, -2); assertEquals (5, sum); } }
  • 40. Summarizing… an example (Junit 3.x) Test case 2 import junit.framework.TestCase; import math.Add; public class TestCase1_add_Junit3 extends TestCase { public void testAdd() {… } public void testAdd_2() { Add add= new Add(); int sum=add. sum (3, -2); assertEquals (5, sum); } }
  • 41. public class MySum { public static int sum (int a, int b) { return a+b; } } Summarizing… an example (Junit 4.x) import math.Add; import org.junit.*; import static org.junit.Assert.*; public class Testcase1_add_Junit4 { @Test public void testAdd() { Add add= new Add(); int sum=add. sum (3, 2); assertEquals (5, sum); } }
  • 42.
  • 43. Test last New functionality Understand Implement functionality Write tests Run all tests Result? Rework fail pass Next functionality
  • 44. Test first “ Extreme programming” ( XP ) champions the use of tests as a development tool … New functionality Understand Add a single test Add code for the test Run all test Result? Rework Functionality complete? fail pass No Next functionality Yes “ Extreme programming” ( XP ) champions the use of tests as a development tool …
  • 45.
  • 46. Add the skeleton of the class and methods (without body) Test-first with Junit Rework Refactoring “ improving the structure” Add a testcase Run test Run test
  • 47.
  • 49. Add Testcases for the settlement method class Test_CurrentAccount extends TestCase { public void test_settlementVoid () { CurrentAccount c = new CurrentAccount(); assertEquals (0, c.settlement()); } public void test_settlement () { CurrentAccount c = new CurrentAccount(); c.deposit(12); c.draw(-8); c.deposit(10); assertEquals (14, c.settlement()); } } test first …
  • 50. Add the skeleton of the class and methods (without body)
  • 51. Add the skeletone code of the method class CurrentAccount { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[10]; } public void deposit (int value){ account[lastMove]=value; lastMove++; } public void draw (int value) { account[lastMove]=value; lastMove++; } public int settlement () {return 0;} public static void main (String args[]) {} } class Test_CurrentAccount extends TestCase { public void test_settlementVoid () { currentAccount c = new currentAccount(); assertEquals (0, c.settlement()); } public void test_settlement () { currentAccount c = new currentAccount(); c.deposit(12); c.draw(-8); c.deposit(10); assertEquals (14, c.settlement()); } }
  • 53. Run Junit ( first time )
  • 55. Rework class CurrentAccount { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[10]; } public void deposit (int value){ …} public void draw (int value) { …} public int settlement () { int result = 0 for (int i=0; i<account.length; i++) { result = result + account[i]; } return result; } public static void main (String args[]) {} } class Test_CurrentAccount extends TestCase { public void test_settlementVoid () { currentAccount c = new currentAccount(); assertEquals (0, c.settlement()); } public void test_settlement () { currentAccount c = new currentAccount(); c.deposit(12); c.draw(-8); c.deposit(10); assertEquals (14, c.settlement()); } }
  • 57. Run Junit ( second time )
  • 59. Add a new testcase class CurrentAccount { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[10]; } public void deposit (int value){ …} public void draw (int value) { …} public int settlement () { int result = 0 for (int i=0; i<account.length; i++) { result = result + account[i]; } return result; } public static void main (String args[]) {} } class Test_currentAccount extends TestCase { … public void test_realCaseSettlement () { currentAccount c = new currentAccount(); for (int i=0; i <10 ; i++) c.deposit(1); c.draw(-10); assertEquals (0, c.settlement()); } }
  • 61. Run JUnit ( third time ) Run time error
  • 63. class CurrentAccount { int account[]; int lastMove; CurrentAccount (){ lastMove=0; account=new int[ 100 ]; } public void deposit (int value){ …} public void draw (int value) { …} public int settlement () { int result = 0 for (int i=0; i<account.length; i++) { result = result + account[i]; } return result; } public static void main (String args[]) {} } class Test_currentAccount extends TestCase { … public void test_realCaseSettlement () { currentAccount c = new currentAccount(); for (int i=0; i <10 ; i++) c.deposit(1); c.draw(-10); assertTrue (0, c.settlement()); } } Rework
  • 65. Run JUnit ( fourth time )
  • 66. Refactoring “ improving the structure”
  • 67. Refactoring “ changing the data structure: Array --> List” public class CurrentAccount { List account = new LinkedList(); public void deposit (int value) { account.add(new Integer(value)); } public void draw (int value) { account.add(new Integer(value)); } public int settlement () { int result = 0; Iterator it=account.iterator(); while (it.hasNext()) { Integer value_integer = (Integer)it.next(); int val = value_integer.intValue(); result = result + val; } return result; } }
  • 69. Run JUnit ( fifth time )
  • 71.
  • 72.
  • 73.