MockSniffer: Characterizing and Recommending Mocking Decisions for Unit Tests

In unit testing, mocking is popularly used to ease test effort, reduce test flakiness, and increase test coverage by replacing the actual dependencies with simple implementations. However, there are no clear criteria to determine which dependencies in a unit test should be mocked. Inappropriate mocking can have undesirable consequences: under-mocking could result in the inability to isolate the class under test (CUT) from its dependencies while over-mocking increases the developers' burden on maintaining the mocked objects and may lead to spurious test failures. According to existing work, various factors can determine whether a dependency should be mocked. As a result, mocking decisions are often difficult to make in practice. Studies on the evolution of mocked objects also showed that developers tend to change their mocking decisions: 17% of the studied mocked objects were introduced sometime after the test scripts were created and another 13% of the originally mocked objects eventually became unmocked. In this work, we are motivated to develop an automated technique to make mocking recommendations to facilitate unit testing. We studied 10,846 test scripts in four actively maintained open-source projects that use mocked objects, aiming to characterize the dependencies thatare mocked in unit testing. Based on our observations on mocking practices, we designed and implemented a tool, MockSniffer, to identify and recommend mocks for unit tests. The tool is fully automated and requires only the CUT and its dependencies as input. It leverages machine learning techniques to make mocking recommendations by holistically considering multiple factors that can affect developers' mocking decisions. Our evaluation of Mock-Sniffer on ten open-source projects showed that it outperformed three baseline approaches, and achieved good performance in two potential application scenarios.

[1]  Paolo Tonella,et al.  AUTOMOCK: Automated Synthesis of a Mock Environment for Test Case Generation , 2010, Practical Software Testing: Tool Automation and Human Factors.

[2]  Wei-Yin Loh,et al.  Classification and regression trees , 2011, WIREs Data Mining Knowl. Discov..

[3]  Yoav Freund,et al.  A decision-theoretic generalization of on-line learning and an application to boosting , 1995, EuroCOLT.

[4]  Gordon Fraser,et al.  Generating TCP/UDP network data for automated unit test generation , 2015, ESEC/SIGSOFT FSE.

[5]  Jens Dietrich,et al.  Generating Mock Skeletons for Lightweight Web-Service Testing , 2019, 2019 26th Asia-Pacific Software Engineering Conference (APSEC).

[6]  Laurie Hendren,et al.  Soot: a Java bytecode optimization framework , 2010, CASCON.

[7]  Alberto Bacchelli,et al.  Mock objects for testing java systems - Why and how developers use them, and how they evolve , 2019, Empir. Softw. Eng..

[8]  Mauricio Finavaro Aniche,et al.  To Mock or Not to Mock? An Empirical Study on Mocking Practices , 2017, 2017 IEEE/ACM 14th International Conference on Mining Software Repositories (MSR).

[9]  H. B. Mann,et al.  On a Test of Whether one of Two Random Variables is Stochastically Larger than the Other , 1947 .

[10]  Darko Marinov,et al.  An empirical analysis of flaky tests , 2014, SIGSOFT FSE.

[11]  Steve Freeman,et al.  Endo-testing: unit testing with mock objects , 2001 .

[12]  K. Pearson On the Criterion that a Given System of Deviations from the Probable in the Case of a Correlated System of Variables is Such that it Can be Reasonably Supposed to have Arisen from Random Sampling , 1900 .

[13]  Xiaoyin Wang,et al.  An Empirical Study on the Usage of Mocking Frameworks in Software Testing , 2014, 2014 14th International Conference on Quality Software.

[14]  Karl Pearson F.R.S. X. On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling , 2009 .

[15]  Leo Breiman,et al.  Bias, Variance , And Arcing Classifiers , 1996 .

[16]  J. Friedman Greedy function approximation: A gradient boosting machine. , 2001 .

[17]  Gordon Fraser,et al.  Automated unit test generation for classes with environment dependencies , 2014, ASE.

[18]  A. Azzouz 2011 , 2020, City.

[19]  Ming Wen,et al.  ChangeLocator: locate crash-inducing changes based on crash reports , 2017, Empirical Software Engineering.

[20]  Gordon Fraser,et al.  EvoSuite: automatic test suite generation for object-oriented software , 2011, ESEC/FSE '11.

[21]  Franz Wotawa,et al.  Automatically extracting mock object behavior from Design by Contract#8482; specification for test data generation , 2010, AST '10.

[22]  K M Søndergaard,et al.  [Understanding statistics?]. , 1995, Ugeskrift for laeger.

[23]  Nikolai Tillmann,et al.  Mock-object generation with behavior , 2006, 21st IEEE/ACM International Conference on Automated Software Engineering (ASE'06).

[24]  Nikolai Tillmann,et al.  An empirical study of testing file-system-dependent software with mock objects , 2009, 2009 ICSE Workshop on Automation of Software Test.

[25]  Gordon Fraser,et al.  Private API Access and Functional Mocking in Automated Unit Test Generation , 2017, 2017 IEEE International Conference on Software Testing, Verification and Validation (ICST).

[26]  Gaël Varoquaux,et al.  Scikit-learn: Machine Learning in Python , 2011, J. Mach. Learn. Res..

[27]  Bernhard E. Boser,et al.  A training algorithm for optimal margin classifiers , 1992, COLT '92.

[28]  Alfred V. Aho,et al.  Compilers: Principles, Techniques, and Tools , 1986, Addison-Wesley series in computer science / World student series edition.

[29]  Gordon Fraser,et al.  Do Automatically Generated Unit Tests Find Real Faults? An Empirical Study of Effectiveness and Challenges (T) , 2015, 2015 30th IEEE/ACM International Conference on Automated Software Engineering (ASE).

[30]  Harry Zhang,et al.  The Optimality of Naive Bayes , 2004, FLAIRS.

[31]  Yi Zhang,et al.  MODA: automated test generation for database applications via mock objects , 2010, ASE '10.