Citizendia
Your Ad Here

In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. Object-oriented programming (OOP is a Programming paradigm that uses " objects " and their interactions to design applications and computer programs A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to test the behavior of a car during an accident. For the band see Crash Test Dummies. For the series of toys see The Incredible Crash Dummies.

Contents

Reasons for use

In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is difficult or impossible to incorporate into a unit test. In Computer programming, unit testing is a method of testing that verifies the individual units of Source code are working properly If an object has any of the following characteristics, it may be useful to use a mock object in its place:

For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock object is used in place of the real object, it can be programmed to provide the bell-ringing time (whether it is actually that time or not) so that the alarm clock program can be tested in isolation.

Technical details

Mock objects have the same interface as the real objects they mimic, allowing a client object to remain unaware of whether it is using a real object or a mock object. Interface generally refers to an abstraction that an entity provides of itself to the outside Many available mock object frameworks allow the programmer to specify which, and in what order, methods will be invoked on a mock object and what parameters will be passed to them, as well as what values will be returned. In Object-oriented programming, the term method refers to a Subroutine that is exclusively associated either with a class (called class methods In Computer programming, a parameter is a variable which takes on the meaning of a corresponding Argument (computer science is same article--> argument Thus, the behavior of a complex object such as a network socket can be mimicked by a mock object, allowing the programmer to discover whether the object being tested responds appropriately to the wide variety of states such objects may be in.

Mocks and fakes

Some authors[1] draw a distinction between fake and mock objects. Fakes are the simpler of the two, simply implementing the same interface as the object that they represent and returning pre-arranged responses. Mock objects in this sense do a little more: their method implementations contain assertions of their own. In Computer programming, an assertion is a predicate (ie a true–false statement placed in a program to indicate that the developer thinks that the This means that a true mock, in this sense, will examine the context of each call—perhaps checking the order in which its methods are called, perhaps performing tests on the data passed into the method calls as arguments.

Setting expectations

Consider an example where an authorization sub-system has been mocked. The mock object implements an isUserAllowed(task : Task) : boolean[2] method to match that in the real authorization class. Many advantages follow if it also exposes an isAllowed : boolean property which is not present in the real class. This allows test code easily to set the expectation that a user will, or will not, be granted permission in the next call and therefore readily to test the behaviour of the rest of the system in either case.

Writing log strings

A mock database object's save(person : Person) method may contain not much of any implementation code. It might or might not check the existence and perhaps the validity of the Person object passed in for saving (see fake vs. mock discussion above), but beyond that there might be no other implementation.

This would be to miss an opportunity. The mock method could add an entry to a public log string. The entry need be no more than "Person saved\n"[3], or it may include some details from the person object instance, such as a name or ID. If the test code also checks the final contents of the log string after various series of operations involving the mock database then it is possible to verify that in each case exactly the expected number of database saves have been performed. This can find otherwise invisible performance-sapping bugs, for example, where a developer, nervous of losing data, has coded repeated calls to save() where just one would have sufficed.

Use in test-driven development

Programmers working with the test-driven development (TDD) methodology make use of mock objects when writing software. Test-Driven Development (TDD is a Software development technique consisting of short iterations where new Test cases covering the desired improvement or Mock objects meet the interface requirements of, and stand in for, more complex real ones; thus they allow programmers to write and unit-test functionality in one area without actually calling complex underlying or collaborating classes[4]. Interface generally refers to an abstraction that an entity provides of itself to the outside In Computer programming, unit testing is a method of testing that verifies the individual units of Source code are working properly In Object-oriented programming, a class is a Programming language construct that is used as a blueprint to create objects This blueprint includes attributes Using mock objects allows developers to focus their tests on the behavior of the system under test (SUT) without worrying about its dependencies. For example, testing a complex algorithm based on multiple objects being in particular states can be clearly expressed using mock objects in place of real objects.

Apart from complexity issues and the benefits gained from this separation of concerns, there are practical speed issues involved. In Computer science, separation of concerns ( SoC) is the process of breaking a Computer program into distinct features that overlap in functionality as Developing a realistic piece of software using TDD may easily involve several hundred unit tests. If many of these induce communication with databases, web services and other out-of-process or networked systems, then the suite of unit tests will quickly become too slow to be run regularly. Inter-Process Communication ( IPC) is a set of techniques for the exchange of data among two or more threads in one or more processes. A computer network is a group of interconnected Computers. Networks may be classified according to a wide variety of characteristics This in turn leads to bad habits and a reluctance by the developer to maintain the basic tenets of TDD.

When mock objects are replaced by real ones then the end-to-end functionality will need further testing. These will be integration tests rather than unit tests. 'Integration testing'(sometimes called Integration and Testing abbreviated I&T) is the phase of Software testing in which individual software modules are combined and tested

See also

References

  1. ^ Feathers, Michael (2005). A method stub or simply stub in Software development is a piece of code used to stand in for some other programming functionality Virtual Mocks are a way to mimic the behavior of real objects in a controlled way "Sensing and separation", Working effectively with legacy code. NJ: Prentice Hall, Pp 23 et seq. ISBN 0-13-117705-2.  
  2. ^ These examples use a nomenclature that is similar to that used in Universal Modeling Language
  3. ^ Beck, Kent (2003). the Unified Modeling Language (UML a class diagram is a type of static structure diagram that describes the structure of a system by showing the system's classes Test-Driven Development By Example. Boston: Addison Wesley, Pp. 146-7. ISBN 0-321-14653-0.  
  4. ^ Beck, Kent (2003). Test-Driven Development By Example. Boston: Addison Wesley, Pp. 144-5. ISBN 0-321-14653-0.  

External links



© 2009 citizendia.org; parts available under the terms of GNU Free Documentation License, from http://en.wikipedia.org
Dapyx Software network: MP3 Explorer | Ebook Manager | Zenithic