To Mock Or Not To Mock?
An Empirical Study on Mocking Practices
Davide Spadini, Mauricio Aniche, Magiel Bruntink, Alberto Bacchelli @DavideSpadini
To Mock Or Not To Mock? An Empirical Study on Mocking Practices - - PowerPoint PPT Presentation
To Mock Or Not To Mock? An Empirical Study on Mocking Practices Davide Spadini , Mauricio Aniche, Magiel Bruntink, Alberto Bacchelli @DavideSpadini How do I test this? With dependencies Without dependencies Mock Unit under test Unit
An Empirical Study on Mocking Practices
Davide Spadini, Mauricio Aniche, Magiel Bruntink, Alberto Bacchelli @DavideSpadini
With dependencies Without dependencies
Unit under test Unit under test
RQ1: What test dependencies do developers mock? RQ2: Why do developers decide to (not) mock specific dependencies? RQ3: Which are the main challenges experienced with testing using mocks?
Opensource Industrial
TA P1 Sonarqube Alura VRaptor Spring Framework TA P1 TA P1 TA P1
TA MockExtractor TA P3 Mock P1 P2
13,547 dependencies
P1 Sonarqube TA P3 Mock P1 P2
1,665 dependencies
Alura TA P3 Mock P1 P2
1,333 dependencies
VRaptor TA P3 Mock P1 P2
21,768 dependencies
Spring Framework TA P1 TA P1 TA P1
TA MockExtractor TA P3 Mock P1 P2
13,547 dependencies 674 dependencies
TA P1 Tn Pn … … Sampling P1 Sonarqube TA P3 Mock P1 P2
1,665 dependencies 445 dependencies
TA P1 Tn Pn … … Alura TA P3 Mock P1 P2
1,333 dependencies 438 dependencies
TA P1 Tn Pn … … VRaptor TA P3 Mock P1 P2
21,768 dependencies 621 dependencies
TA P1 Tn Pn … … Spring Framework
Data collection
TA P1 TA P1 TA P1
TA MockExtractor TA P3 Mock P1 P2
13,547 dependencies 674 dependencies
TA P1 Tn Pn … … Sampling
Sample 1
P1 Sonarqube TA P3 Mock P1 P2
1,665 dependencies 445 dependencies
TA P1 Tn Pn … … Alura TA P3 Mock P1 P2
1,333 dependencies 438 dependencies
TA P1 Tn Pn … … VRaptor TA P3 Mock P1 P2
21,768 dependencies 621 dependencies
TA P1 Tn Pn … … Spring Framework
Sample 2
Validation Manual analysis (2nd author) Manual analysis (1st author)
Data analysis Data collection Manual analysis
TA P1 TA P1 TA P1
TA MockExtractor TA P3 Mock P1 P2
13,547 dependencies 674 dependencies
TA P1 Tn Pn … … Sampling
Commit Comments Commit Comments Commit Comments
132 categories
Commit Comments
7
Categories
Sample 1
P1 Sonarqube TA P3 Mock P1 P2
1,665 dependencies 445 dependencies
TA P1 Tn Pn … … Alura TA P3 Mock P1 P2
1,333 dependencies 438 dependencies
TA P1 Tn Pn … … VRaptor TA P3 Mock P1 P2
21,768 dependencies 621 dependencies
TA P1 Tn Pn … … Spring Framework
Sample 2
Validation Manual analysis (2nd author) Manual analysis (1st author) Discussion
Data analysis Data collection Manual analysis Categorisation
TA P1 TA P1 TA P1
0% 25% 50% 75% 100% Database Web service External Dependencies Domain
Java libraries Test support
94% 93% 64% 32% 31% 28%
6% 7% 36% 68% 69% 72%
Mocked Not mocked
TA MockExtractor TA P3 Mock P1 P2
13,547 dependencies 674 dependencies
TA P1 Tn Pn … … Sampling
Commit Comments Commit Comments Commit Comments
132 categories
Commit Comments
7
Categories
Interview Guideline 3 developers
Sample 1
P1 Sonarqube TA P3 Mock P1 P2
1,665 dependencies 445 dependencies
TA P1 Tn Pn … … Alura TA P3 Mock P1 P2
1,333 dependencies 438 dependencies
TA P1 Tn Pn … … VRaptor TA P3 Mock P1 P2
21,768 dependencies 621 dependencies
TA P1 Tn Pn … … Spring Framework
Sample 2
Validation Manual analysis (2nd author) Manual analysis (1st author) Discussion
Data analysis Interviews & Validation Data collection Manual analysis Categorisation
TA P1 TA P1 TA P1
Not when the focus of the test is the integration
Vehicle Car Plane Boat
Mock interfaces rather than a specific implementation Concrete implementation is not easy
TA MockExtractor TA P3 Mock P1 P2
13,547 dependencies 674 dependencies
TA P1 Tn Pn … … Sampling
Commit Comments Commit Comments Commit Comments
132 categories
Commit Comments
7
Categories
Interview Guideline 3 developers
Commit Comments Review Comment Commit Comments Interview Transcript Commit Comments Commit Comments
Survey (105 respondents)
Sample 1
P1 Sonarqube TA P3 Mock P1 P2
1,665 dependencies 445 dependencies
TA P1 Tn Pn … … Alura TA P3 Mock P1 P2
1,333 dependencies 438 dependencies
TA P1 Tn Pn … … VRaptor TA P3 Mock P1 P2
21,768 dependencies 621 dependencies
TA P1 Tn Pn … … Spring Framework
Sample 2
Validation Manual analysis (2nd author) Manual analysis (1st author) Discussion
Affinity Diagram
Data analysis Interviews & Validation Data collection Manual analysis Categorisation
TA P1 TA P1 TA P1
Legacy systems Dealing with coupling Production quality code vs mocks
Mockito developer
do more?
Open questions
mocks?