SlideShare une entreprise Scribd logo
1  sur  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]

Contenu connexe

Tendances (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
 

Similaire à 3 j unit

Similaire à 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
 

Dernier

Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 

Dernier (20)

Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 

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.