SlideShare a Scribd company logo
JUnit Recipes
- Elementary tests
   Zheng-Wen Shen
     2007/10/25



                     1
Brief contents
    Part 1: The Building Blocks
    1.   Fundamentals
    2.   Elementary tests
    3.   Organizing and building JUnit tests
    4.   Managing test suites
    5.   Working with test data
    6.   Running JUnit tests
    7.   Reporting JUnit results
    8.   Troubleshooting JUnit
    Part 2: Testing J2EE
    Part 3: More JUnit Techniques

                                               2
The Building Blocks
            Elementary tests
1. Test your equals methods
2. Test a method that returns nothing
3. Test a constructor
4. Test a getter
5. Test a setter
6. Test an interface
7. Test throwing the right exception
8. Let collections compare themselves
9. Test a big object for equality
10.Test an object that instantiates other   objects

                                                      3
The Building Blocks
            Elementary tests
1. Test your equals methods
2. Test a method that returns nothing
3. Test a constructor
4. Test a getter
5. Test a setter
6. Test an interface
7. Test throwing the right exception
8. Let collections compare themselves
9. Test a big object for equality
10.Test an object that instantiates other   objects

                                                      4
1. Test your equals methods (1/3)
                      Test the implementation of equals()

 Value         Object: represents a value
       String, Integer, Double, Money, Timestamp,
        etc.
Money   a= new Money(100,0);
Money   b= new Money(100,0);            a
Money   c= new Money(50,0);                           b
Money   d= new Money(50,0);           100, 0
                                                    100, 0
a.equals( b ); // true
a.equals( c ); // false
c.equals( d ); // true                  c
c.equals( a ); // false
                                      50, 0           d
                                                    50, 0


                                                             5
1. Test your equals methods (2/3)
               Test the implementation of equals()

 The   contract of equals()
     Equivalence relations (RST)
       • Reflexive property
       • Symmetric property
       • Transitive property
     Consistent
     No object equals null



                                                     6
1. Test your equals methods (3/3)
                 Test the implementation of equals()




 equal to a
not equal to a
“looks equal”




 JUnit utility

                          RST, consistent, no object is equal null…



                                                                      7
2. Test a method that returns nothing
                                        (1/3)

   “How  do I test a method that return void?”
   If a method returns no value, it must have
    some observable side effect!

xyz obj = new xyz();                       xyz object state
obj.change();   //   transit   to   A
obj.change();   //   transit   to   B   Start        A
obj.change();   //   transit   to   C
obj.change();   //   transit   to   D



                                                B             C

                                                                  8
2. Test a method that returns nothing
                                    (2/3)

             To test the Collection.add(Object)
         1.      Create an empty collection
         2.      The collection should not contain the item in question
         3.      Add the item in question
         4.      Now the collection should contain the item in question


                   1
                   2
return nothing     3
                   4


                                                                    9
2. Test a method that returns nothing
                            (3/3)

          We are testing behavior, and not methods.
          If code does the wrong thing but no test
           fails, does it have as defect?

  
NOTE       The tests are the specification!!
            We describe what our code does by providing
             the tests our code passes.


                                                     10
3. Test a constructor

   Uses exposed internal state



   Observable side effect



   Pitfalls

                                       11
4. Test a getter (1/3)
    Which tests are needed and which are not

   Do not test methods that too simple to
    break!!




     too simple




                                               12
4. Test a getter (2/3)
    Which tests are needed and which are not

   Compare that result with an expected
    value




                                               13
4. Test a getter (3/3)
         Which tests are needed and which are not
  An alternative implementation…




Too simple to break




                                                    14
5. Test a setter (1/3)
                 Should I test my set methods?
         Basic set methods are too simple to break
         Effective way to test if you have to:

pattern                                     1
             2
             3
             4


      1.    Name the test method appropriately
      2.    Create an instance of your bean class
      3.    If newPropertyValue is a complex property, then initialize
            newPropertyValue.
      4.    If property is a more complex object than string, then you need
            to ensure that equals() is appropriately implemented
                                                                          15
5. Test a setter (2/3)
          Should I test my set methods?
                             If there are no get methods…




Command

                              Analyze the side effect



                                                        16
5. Test a setter (3/3)
              Should I test my set methods?
    BankTransferAction action = new BankTransferAction();
    action.setSourceAccountId("source");
    action.setTargetAccountId("target");
    action.setAmount(Money.dollars(100));


                                                             Bank

“Spy”
                                                            Subclass
                                                            Of Bank
                                                             (Spy)




                                                                17
The Building Blocks
                     Elementary tests
1.   Test your equals methods
2.   Test a method that returns nothing
3.   Test a constructor
4.   Test a getter
5.   Test a setter                         to be continue…
6. Test an interface
7. Test throwing the right exception
8. Let collections compare themselves
9. Test a big object for equality
10.Test an object that instantiates other objects




                                                         18

More Related Content

What's hot

Control structures i
Control structures i Control structures i
Control structures i
Ahmad Idrees
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
It Academy
 
New Features Of Test Unit 2.x
New Features Of Test Unit 2.xNew Features Of Test Unit 2.x
New Features Of Test Unit 2.x
djberg96
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
teach4uin
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
Keshav Kumar
 
Mutation Testing - Ruby Edition
Mutation Testing - Ruby EditionMutation Testing - Ruby Edition
Mutation Testing - Ruby Edition
Chris Sinjakli
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
inTwentyEight Minutes
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
jyoti_lakhani
 
Mutation Testing
Mutation TestingMutation Testing
Mutation Testing
Chris Sinjakli
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
Valerio Maggio
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
Savitribai Phule Pune University
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
Nayanda Haberty
 
Junit 4.0
Junit 4.0Junit 4.0

What's hot (19)

Control structures i
Control structures i Control structures i
Control structures i
 
Exceptions handling notes in JAVA
Exceptions handling notes in JAVAExceptions handling notes in JAVA
Exceptions handling notes in JAVA
 
Mock object
Mock objectMock object
Mock object
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Unit testing basic
Unit testing basicUnit testing basic
Unit testing basic
 
New Features Of Test Unit 2.x
New Features Of Test Unit 2.xNew Features Of Test Unit 2.x
New Features Of Test Unit 2.x
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
 
Mutation Testing - Ruby Edition
Mutation Testing - Ruby EditionMutation Testing - Ruby Edition
Mutation Testing - Ruby Edition
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
Mutation Testing
Mutation TestingMutation Testing
Mutation Testing
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Unit testing with Junit
Unit testing with JunitUnit testing with Junit
Unit testing with Junit
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
Junit 4.0
Junit 4.0Junit 4.0
Junit 4.0
 

Viewers also liked

What's Up 1 Test 3 (versions a & b)
What's Up 1 Test 3 (versions a & b)What's Up 1 Test 3 (versions a & b)
What's Up 1 Test 3 (versions a & b)
Silvia García Iturzaeta
 
Pet speaking exam
Pet speaking examPet speaking exam
Pet speaking exam
Isabelitamellamo
 
Pet speaking questions with answers
Pet speaking questions   with answersPet speaking questions   with answers
Pet speaking questions with answersloe11
 
Pet and fce speaking paper part 1
Pet and fce  speaking paper part 1Pet and fce  speaking paper part 1
Pet and fce speaking paper part 1Javier Martos
 
Picture description
Picture descriptionPicture description
Picture description
Maribel Gonzalez
 
Storyboard elementary comparison_test
Storyboard elementary comparison_testStoryboard elementary comparison_test
Storyboard elementary comparison_test
SogetiNL
 
Comparing and Contrasting photos
Comparing and Contrasting photosComparing and Contrasting photos
Comparing and Contrasting photos
David Mainwood
 

Viewers also liked (10)

What's Up 1 Test 3 (versions a & b)
What's Up 1 Test 3 (versions a & b)What's Up 1 Test 3 (versions a & b)
What's Up 1 Test 3 (versions a & b)
 
Test book what's up 1b
Test book what's up 1bTest book what's up 1b
Test book what's up 1b
 
Pet speaking exam
Pet speaking examPet speaking exam
Pet speaking exam
 
Pet speaking questions with answers
Pet speaking questions   with answersPet speaking questions   with answers
Pet speaking questions with answers
 
Pet and fce speaking paper part 1
Pet and fce  speaking paper part 1Pet and fce  speaking paper part 1
Pet and fce speaking paper part 1
 
Test book what's up 1
Test book what's up 1Test book what's up 1
Test book what's up 1
 
Pet speaking paper
Pet speaking paperPet speaking paper
Pet speaking paper
 
Picture description
Picture descriptionPicture description
Picture description
 
Storyboard elementary comparison_test
Storyboard elementary comparison_testStoryboard elementary comparison_test
Storyboard elementary comparison_test
 
Comparing and Contrasting photos
Comparing and Contrasting photosComparing and Contrasting photos
Comparing and Contrasting photos
 

Similar to Junit Recipes - Elementary tests (1/2)

Junit Recipes - Intro
Junit Recipes - IntroJunit Recipes - Intro
Junit Recipes - IntroWill Shen
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
Sbin m
 
Integration testing
Integration testingIntegration testing
Integration testing
Tsegabrehan Am
 
Easy mockppt
Easy mockpptEasy mockppt
Easy mockppt
subha chandra
 
Easy mock
Easy mockEasy mock
Easy mock
Ramakrishna kapa
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
Narendra Pathai
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
Amr E. Mohamed
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
Easy mock
Easy mockEasy mock
Easy mock
Srikrishna k
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
Keshav Kumar
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
Paul Churchward
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
Amr E. Mohamed
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 world
Yura Nosenko
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
Aktuğ Urun
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
JWORKS powered by Ordina
 
05 junit
05 junit05 junit
05 junit
mha4
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with MockitoRichard Paul
 
Test driven development
Test driven developmentTest driven development
Test driven developmentJohn Walsh
 
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf
gauravavam
 
Testing object oriented software.pptx
Testing object oriented software.pptxTesting object oriented software.pptx
Testing object oriented software.pptx
DRPOONAMDRPOONAM1
 

Similar to Junit Recipes - Elementary tests (1/2) (20)

Junit Recipes - Intro
Junit Recipes - IntroJunit Recipes - Intro
Junit Recipes - Intro
 
Easymock Tutorial
Easymock TutorialEasymock Tutorial
Easymock Tutorial
 
Integration testing
Integration testingIntegration testing
Integration testing
 
Easy mockppt
Easy mockpptEasy mockppt
Easy mockppt
 
Easy mock
Easy mockEasy mock
Easy mock
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
 
SE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and JunitSE2_Lec 21_ TDD and Junit
SE2_Lec 21_ TDD and Junit
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Easy mock
Easy mockEasy mock
Easy mock
 
JAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programmingJAVA Tutorial- Do's and Don'ts of Java programming
JAVA Tutorial- Do's and Don'ts of Java programming
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
 
SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)SE2018_Lec 20_ Test-Driven Development (TDD)
SE2018_Lec 20_ Test-Driven Development (TDD)
 
Testing Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 worldTesting Spring Boot application in post-JUnit 4 world
Testing Spring Boot application in post-JUnit 4 world
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
05 junit
05 junit05 junit
05 junit
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf
 
Testing object oriented software.pptx
Testing object oriented software.pptxTesting object oriented software.pptx
Testing object oriented software.pptx
 

More from Will Shen

20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)
20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)
20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)
Will Shen
 
16格筆記讀書法
16格筆記讀書法16格筆記讀書法
16格筆記讀書法
Will Shen
 
Bade Smells in Code
Bade Smells in CodeBade Smells in Code
Bade Smells in Code
Will Shen
 
Intro To BOOST.Spirit
Intro To BOOST.SpiritIntro To BOOST.Spirit
Intro To BOOST.Spirit
Will Shen
 
20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...
Will Shen
 
20060411 face recognition using face arg matching
20060411 face recognition using face arg matching20060411 face recognition using face arg matching
20060411 face recognition using face arg matching
Will Shen
 
20060411 Analytic Hierarchy Process (AHP)
20060411 Analytic Hierarchy Process (AHP)20060411 Analytic Hierarchy Process (AHP)
20060411 Analytic Hierarchy Process (AHP)
Will Shen
 
20050713 critical paths for gui regression testing
20050713 critical paths for gui regression testing20050713 critical paths for gui regression testing
20050713 critical paths for gui regression testing
Will Shen
 
20050314 specification based regression test selection with risk analysis
20050314 specification based regression test selection with risk analysis20050314 specification based regression test selection with risk analysis
20050314 specification based regression test selection with risk analysis
Will Shen
 
20041113 A Test Generation Tool for Specifications in the Form of State Machine
20041113 A Test Generation Tool for Specifications in the Form of State Machine20041113 A Test Generation Tool for Specifications in the Form of State Machine
20041113 A Test Generation Tool for Specifications in the Form of State Machine
Will Shen
 
20051019 automating regression testing for evolving gui software
20051019 automating regression testing for evolving gui software20051019 automating regression testing for evolving gui software
20051019 automating regression testing for evolving gui software
Will Shen
 
20060712 automated model based testing of community-driven open-source gui ap...
20060712 automated model based testing of community-driven open-source gui ap...20060712 automated model based testing of community-driven open-source gui ap...
20060712 automated model based testing of community-driven open-source gui ap...
Will Shen
 
20041221 gui testing survey
20041221 gui testing survey20041221 gui testing survey
20041221 gui testing survey
Will Shen
 
20060927 application facades
20060927 application facades20060927 application facades
20060927 application facades
Will Shen
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtestWill Shen
 
Data collection for field studies
Data collection for field studiesData collection for field studies
Data collection for field studiesWill Shen
 

More from Will Shen (16)

20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)
20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)
20180717 Introduction of Seamless BLE Connection Migration System (SeamBlue)
 
16格筆記讀書法
16格筆記讀書法16格筆記讀書法
16格筆記讀書法
 
Bade Smells in Code
Bade Smells in CodeBade Smells in Code
Bade Smells in Code
 
Intro To BOOST.Spirit
Intro To BOOST.SpiritIntro To BOOST.Spirit
Intro To BOOST.Spirit
 
20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...20070514 introduction to test ng and its application for test driven gui deve...
20070514 introduction to test ng and its application for test driven gui deve...
 
20060411 face recognition using face arg matching
20060411 face recognition using face arg matching20060411 face recognition using face arg matching
20060411 face recognition using face arg matching
 
20060411 Analytic Hierarchy Process (AHP)
20060411 Analytic Hierarchy Process (AHP)20060411 Analytic Hierarchy Process (AHP)
20060411 Analytic Hierarchy Process (AHP)
 
20050713 critical paths for gui regression testing
20050713 critical paths for gui regression testing20050713 critical paths for gui regression testing
20050713 critical paths for gui regression testing
 
20050314 specification based regression test selection with risk analysis
20050314 specification based regression test selection with risk analysis20050314 specification based regression test selection with risk analysis
20050314 specification based regression test selection with risk analysis
 
20041113 A Test Generation Tool for Specifications in the Form of State Machine
20041113 A Test Generation Tool for Specifications in the Form of State Machine20041113 A Test Generation Tool for Specifications in the Form of State Machine
20041113 A Test Generation Tool for Specifications in the Form of State Machine
 
20051019 automating regression testing for evolving gui software
20051019 automating regression testing for evolving gui software20051019 automating regression testing for evolving gui software
20051019 automating regression testing for evolving gui software
 
20060712 automated model based testing of community-driven open-source gui ap...
20060712 automated model based testing of community-driven open-source gui ap...20060712 automated model based testing of community-driven open-source gui ap...
20060712 automated model based testing of community-driven open-source gui ap...
 
20041221 gui testing survey
20041221 gui testing survey20041221 gui testing survey
20041221 gui testing survey
 
20060927 application facades
20060927 application facades20060927 application facades
20060927 application facades
 
20111018 boost and gtest
20111018 boost and gtest20111018 boost and gtest
20111018 boost and gtest
 
Data collection for field studies
Data collection for field studiesData collection for field studies
Data collection for field studies
 

Recently uploaded

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
Alex Pruden
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
Globus
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 

Recently uploaded (20)

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex ProofszkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
zkStudyClub - Reef: Fast Succinct Non-Interactive Zero-Knowledge Regex Proofs
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Enhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZEnhancing Performance with Globus and the Science DMZ
Enhancing Performance with Globus and the Science DMZ
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 

Junit Recipes - Elementary tests (1/2)

  • 1. JUnit Recipes - Elementary tests Zheng-Wen Shen 2007/10/25 1
  • 2. Brief contents  Part 1: The Building Blocks 1. Fundamentals 2. Elementary tests 3. Organizing and building JUnit tests 4. Managing test suites 5. Working with test data 6. Running JUnit tests 7. Reporting JUnit results 8. Troubleshooting JUnit  Part 2: Testing J2EE  Part 3: More JUnit Techniques 2
  • 3. The Building Blocks Elementary tests 1. Test your equals methods 2. Test a method that returns nothing 3. Test a constructor 4. Test a getter 5. Test a setter 6. Test an interface 7. Test throwing the right exception 8. Let collections compare themselves 9. Test a big object for equality 10.Test an object that instantiates other objects 3
  • 4. The Building Blocks Elementary tests 1. Test your equals methods 2. Test a method that returns nothing 3. Test a constructor 4. Test a getter 5. Test a setter 6. Test an interface 7. Test throwing the right exception 8. Let collections compare themselves 9. Test a big object for equality 10.Test an object that instantiates other objects 4
  • 5. 1. Test your equals methods (1/3) Test the implementation of equals()  Value Object: represents a value  String, Integer, Double, Money, Timestamp, etc. Money a= new Money(100,0); Money b= new Money(100,0); a Money c= new Money(50,0); b Money d= new Money(50,0); 100, 0 100, 0 a.equals( b ); // true a.equals( c ); // false c.equals( d ); // true c c.equals( a ); // false 50, 0 d 50, 0 5
  • 6. 1. Test your equals methods (2/3) Test the implementation of equals()  The contract of equals()  Equivalence relations (RST) • Reflexive property • Symmetric property • Transitive property  Consistent  No object equals null 6
  • 7. 1. Test your equals methods (3/3) Test the implementation of equals() equal to a not equal to a “looks equal” JUnit utility RST, consistent, no object is equal null… 7
  • 8. 2. Test a method that returns nothing (1/3)  “How do I test a method that return void?”  If a method returns no value, it must have some observable side effect! xyz obj = new xyz(); xyz object state obj.change(); // transit to A obj.change(); // transit to B Start A obj.change(); // transit to C obj.change(); // transit to D B C 8
  • 9. 2. Test a method that returns nothing (2/3)  To test the Collection.add(Object) 1. Create an empty collection 2. The collection should not contain the item in question 3. Add the item in question 4. Now the collection should contain the item in question 1 2 return nothing 3 4 9
  • 10. 2. Test a method that returns nothing (3/3)  We are testing behavior, and not methods.  If code does the wrong thing but no test fails, does it have as defect?  NOTE The tests are the specification!!  We describe what our code does by providing the tests our code passes. 10
  • 11. 3. Test a constructor  Uses exposed internal state  Observable side effect  Pitfalls 11
  • 12. 4. Test a getter (1/3) Which tests are needed and which are not  Do not test methods that too simple to break!! too simple 12
  • 13. 4. Test a getter (2/3) Which tests are needed and which are not  Compare that result with an expected value 13
  • 14. 4. Test a getter (3/3) Which tests are needed and which are not An alternative implementation… Too simple to break 14
  • 15. 5. Test a setter (1/3) Should I test my set methods?  Basic set methods are too simple to break  Effective way to test if you have to: pattern 1 2 3 4 1. Name the test method appropriately 2. Create an instance of your bean class 3. If newPropertyValue is a complex property, then initialize newPropertyValue. 4. If property is a more complex object than string, then you need to ensure that equals() is appropriately implemented 15
  • 16. 5. Test a setter (2/3) Should I test my set methods? If there are no get methods… Command Analyze the side effect 16
  • 17. 5. Test a setter (3/3) Should I test my set methods? BankTransferAction action = new BankTransferAction(); action.setSourceAccountId("source"); action.setTargetAccountId("target"); action.setAmount(Money.dollars(100)); Bank “Spy” Subclass Of Bank (Spy) 17
  • 18. The Building Blocks Elementary tests 1. Test your equals methods 2. Test a method that returns nothing 3. Test a constructor 4. Test a getter 5. Test a setter to be continue… 6. Test an interface 7. Test throwing the right exception 8. Let collections compare themselves 9. Test a big object for equality 10.Test an object that instantiates other objects 18