Simulate interfaces, classes, and exceptions with open source mock object framework
Test-driven development is an important part of software development. If the code is not tested, it is unreliable. All code must be tested, and ideally you should write your tests before you write your code. However, some things are easy to test and some things are not easy. If you want to write a simple class that represents a currency value, it is easy to test whether adding $1.23 and $2.8 can draw $4.03, not $3.03 or $4.029999998. It is also not difficult to test whether a currency value such as $7.465 is not present. But how do you test the way to convert $7.50 to €5.88 (especially if you're querying a database for any change in exchange rate information?)? The correct results for Amount.toeuros () may change each time you run the program.
The answer is a mock object. Instead of connecting to the real server to get the latest exchange rate information, the test connects a mock server that always returns the same exchange rate. This allows for predictable results that can be tested against it. After all, the goal of the test is the logic in the Toeuros () method, not whether the server sends the correct value. (That's what developers of the build server are worried about). This mock object is sometimes called fake.
Mock objects also help test error conditions. For example, if the Toeuros () method tries to get the latest exchange rate, but the network is disconnected, what happens? You can unplug the ethernet cable from your computer and run the test, but it's much easier to write a mock object that simulates a network failure.
Mock objects can also test the behavior of a class. By placing assertions in mock code, you can examine whether the code being tested passes the appropriate parameters to its collaborators at the appropriate time. You can view and test the private parts of a class by mocks without having to expose them through unnecessary public methods.
Finally, mock objects help you eliminate dependencies from your tests. They make the tests more modular. Failure in a test involving a mock object is most likely a failure in the method being tested, and is unlikely to be a problem in a dependency. This helps isolate problems and simplifies debugging.
EasyMock is an open source mock object Library for the Java programming language that can help you quickly and easily create mock objects for these purposes. EasyMock uses dynamic proxies, allowing you to create a basic implementation of any interface in just one line of code. You can also create a mock for a class by adding the EasyMock class extension. You can configure these mocks for any purpose, from the simple dumb arguments in the method signature to the multiple call tests that verify a series of method calls.
EasyMock Introduction
Now demonstrates how EasyMock works with a specific example. Listing 1 is a fictitious exchangerate interface. Like any interface, an interface only shows what the instance is doing, not how it should be done. For example, it does not specify whether to obtain exchange rate data from Yahoo financial services, government or elsewhere.
Listing 1. ExchangeRate
Import java.io.IOException;
Public interface ExchangeRate {
double getrate (String inputcurrency, String outputcurrency) throws ioexception;
}