After being familiar with JUnit testing, I wanted to look more into Mocking. The idea of mock objects was confusing at first. In class our Prof. covered some examples how to use various mock object frameworks such as EasyMock and Jmockit. So, I thought to do some more readings on it. The article I read focusses on the concept of mocking in general. What is a mock object? What is it used for? Why can’t I mock object XYZ?
In the real world, software has dependencies. We have action classes that depend on services and services that depend on data access objects. The idea of unit testing is that we want to test our code without testing the dependencies. The code below would be an example of this:
import java.util.ArrayList;
public class Counter {
public Counter() {
}
public int count(ArrayList items) {
int results = 0;
for(Object curItem : items) {
results ++;
}
return results;
}
}
If we wanted to test the method count, we would write at test that addressed how the count method works. We aren’t trying to test that ArrayList works because you assume that it has been tested and works as designed. Our only goal is to test your use of ArrayList.
Let’s look at a slightly more realistic example:
public class Action extends ActionSupport {
private LookupService service;
private String key;
public void setKey(String curKey) {
key = curKey;
}
public String getKey() {
return key;
}
public void setService(LookupService curService) {
service = curService;
}
public String doLookup() {
if(StringUtils.isBlank(key)) {
return FAILURE;
}
List results = service.lookupByKey(key);
if(results.size() > 0) {
return SUCCESS;
}
return FAILURE;
}
}
We wanted to test the doLookup method in the above code, we would want to be able to test it without testing the lookupByKey method. For the sake of this test, we assume that the lookupByKey method is tested to work as designed. As long as we pass in the correct key, we will get back the correct results. In reality, we make sure that lookupByKey is also tested if it is code we wrote. How do we test doLookup without executing lookupByKey? The concept behind mock objects is that we want to create an object that will take the place of the real object. This mock object will expect a certain method to be called with certain parameters and when that happens, it will return an expected result. Using the above code as an example, let’s say that when we pass in 1234 for my key to the service.lookupByKey call, we should get back a List with 4 values in it. Our mock object should expect lookupByKey to be called with the parameter “1234” and when that occurs, it will return a List with four objects in it.
After reading the article, the key takeaways for me was to know that mock objects are a very valuable tool in testing. They provide us with the ability to test what we write without having to address dependency concerns. In my coming weeks I will learn about implementing a specific mocking framework.
Source: http://www.michaelminella.com/testing/the-concept-of-mocking.html
From the blog CS@Worcester – Not just another CS blog by osworup007 and used with permission of the author. All other rights reserved by the author.