Unit Testing with Mock Objects Agile Testing

Our unit tests were written by developers in Java code and executed with the JUnit framework. In most cases, the code under test had dependencies on other system components and so, by default, this would have led to the execution (and therefore validation) of a large part of the system with each test. In order to avoid this situation,the developers forcibly stopped the code under test from calling other parts of the system.Instead, it was made to interact with test code that mimicked the original system by using mock objects – Java objects created by the test that are directly interchangeable with the real thing.

In their most basic form, mock objects can be pieces of code written by developers that use programming language mechanisms such as inheritance or interface implementation to make them interchangeable with existing system code.However, mock objects written in this way tend to be unwieldy and hard to maintain.Instead, developers commonly use one of the freely available dynamic mock object frameworks such as EasyMock or Rhino Mocks(for Java and .Net, respectively). Such frameworks aid the test developer by controlling the creation of mock objects and monitoring how the system code attempts to interact with them. Figure illustrates the typical use of a dynamic mock object framework by a unit test.When the test is run , the sequence of events is as follows:

  • The unit test initiates the mock objects framework and requests a dynamic mock for each dependency of the code under test.
  • The mock objects framework creates the dynamic mock(s).
  • Application under test.
  • The unit test creates a set of “expectations” that tells the mocks how it expects the code under test to interact with them and what responses they should provide.
  • The unit test invokes the code under test, ensuring that it will use the dynamic mocks in place of any other system code (i.e.,code that is outside the test scope).
  • The code under test executes, calling the dynamic mock instead of the real thing.
  • The dynamic mock verifies whether it has been called correctly. If so, it returns the canned result it was given. If not, it throws an error that will let the unit test know that the test has failed.

After evaluating some of the existing dynamic mock objects implementations,we chose to use EasyMock.EasyMock allows a developer to create expectations by effectively “recording” a set of calls made to the mock object by test code. The EasyMock framework stores details of these calls but does not act further on them until it is put into “replay” mode. At this point,the framework knows that setup is complete and so it will verify that the calls are repeated by the system under test.

This mechanism is simple to use and has the advantage that the tests will not be broken by refactoring(i.e., making changes that do not affect behavior) of the system under test.For example, if a developer were to use an integrated development environment(IDE) to change the name of a particular operation, the tool would automatically update all references to that operation,including those in the test.Mock objects frameworks that do not use a mechanism like this tend not to be supported so well by common development tools, making their use a little harder.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Agile Testing Topics