Private API Access and Functional Mocking in Automated Unit Test Generation

Private API Access and Functional Mocking in Automated Unit Test Generation
复制标题

DOI:
10.1109/icst.2017.19
复制
发表时间:
2017-03
期刊:
2017 IEEE International Conference on Software Testing, Verification and Validation (ICST)
影响因子:
--
通讯作者:
Andrea Arcuri;G. Fraser;René Just
Andrea Arcuri;G. Fraser;René Just
中科院分区:
其他
文献类型:
--
作者:
Andrea Arcuri;G. Fraser;René Just

文献摘要

被引文献

相似文献

并非所有面向对象的代码都很容易测试:依赖关系对象可能难以实例化,并且面向对象的封装使测试可能无法轻松访问测试可能很简单。当发生这种情况时,开发人员可以求助于模拟复杂依赖项的模拟对象,或直接通过使用Java反射直接使用以对象为导向的封装和访问私有API。自动化的单元测试生成也可以从这些技术中受益吗?在本文中,我们通过扩展EvoSuite单元测试生成工具,具有直接访问私有API并使用流行的Mockito框架创建模拟对象的能力来研究这个问题。但是,需要注意这不会影响生成的测试的有用性:例如,即使该字段重命名,访问私有字段的测试也可能会失败,即使该重命名是语义上具有语义性赋予的Refactoring的一部分。这样的失败不会揭示出真正的回归错误,而是一个假阳性,这浪费了开发人员的调查和修复测试的时间。我们对SF110和缺陷4J基准测试的实验证实了预期的代码覆盖范围和错误查找方面的改善,但也证实了误报的存在。但是,通过确保仅如果没有其他方法来达到代码的某些部分,则仅使用模拟和反射,它们的数字仍然很小。
Not all object oriented code is easily testable: Dependency objects might be difficult or even impossible to instantiate, and object-oriented encapsulation makes testing potentially simple code difficult if it cannot easily be accessed. When this happens, then developers can resort to mock objects that simulate the complex dependencies, or circumvent object-oriented encapsulation and access private APIs directly through the use of, for example, Java reflection. Can automated unit test generation benefit from these techniques as well? In this paper we investigate this question by extending the EvoSuite unit test generation tool with the ability to directly access private APIs and to create mock objects using the popular Mockito framework. However, care needs to be taken that this does not impact the usefulness of the generated tests: For example, a test accessing a private field could later fail if that field is renamed, even if that renaming is part of a semantics-preserving refactoring. Such a failure would not be revealing a true regression bug, but is a false positive, which wastes the developer's time for investigating and fixing the test. Our experiments on the SF110 and Defects4J benchmarks confirm the anticipated improvements in terms of code coverage and bug finding, but also confirm the existence of false positives. However, by ensuring the test generator only uses mocking and reflection if there is no other way to reach some part of the code, their number remains small.