Software QA FYI - SQAFYI

Evolution of an Inside-Outside Test

By: James Grenning

The tests created using Test Driven Design are used to design the interface of a class, provide an example of the use of class and show that the class works. Some tests can reveal the implementation of the class. We often need these tests during the incremental development of the class, allowing development to proceed in small steps. The tests revealing the inside knowledge are undesirable once the class has been designed and is working. When the tests reveal the implementation of the class: the tests do not provide a good model for using the class and the tests are likely to need changes when the underlying implementation is changed. Here is a small case study showing the evolution of a test needing inside knowledge to a test needing only outside knowledge.

Mike and I sat down to add persistence for a class that contains a collection of objects. The object type is not important for this story, so Iíll just call the object Thing. We have a persistence helper class that we used called PersistentObjectCollection. PersistentObjectCollection holds a container of objects that are initially loaded from a file and later saved to a file. We donít want to reveal the persistence mechanism to the rest of the application that uses Thing, so Mike and I decided that a proxy pattern met our needs. The design looks like this.

We were developing using Test Driven Design. We setup a test class for testing our new class ThingStoreProxy using JUnit. Our first test established how to create a ThingStoreProxy and what happens when we create the ThingStoreProxy with a file that does not exist. If we try to get messages from the proxy and the persistence file is empty, we will get a message Collection that is empty. This test also establishes that the proxy data is stored in a file.

//Code snip #1
public class ThingStoreProxyTest extends TestCase {
private String fileName = "test.test";
private File file = new File(fileName);
public ThingStoreProxyTest(String name) {
super(name);
}
public void testCreateEmpty() {
file.delete();
ThingStoreProxy proxy = new ThingStoreProxy(fileName);
Collection things = proxy.getThings();
assertNotNull(things);
assertEquals(0, things.size());
}
}

We decided next to have the proxy created with a file that has data in it. We had a bit of a chicken and egg problem. How can we create a proxy from a persistence file if we never saved persistent data before? Mike had the bright idea that we use the underlying mechanism in PersistentObjectCollection to create a persistence file and initialize from that. Then we can check if the objects that should

Full article...


Other Resource

... to read more articles, visit http://sqa.fyicenter.com/art/

Evolution of an Inside-Outside Test