In Test-Driven Development (TDD), I have a tendency to dive right in at the level of some class that I am sure I’m gonna need for this cool new feature that I’m working on. This has bitten me a few times in the past, where I would start bottom-up and work my way up, only to discover that the design should be a little different and the class I started out with is either not needed, or not needed in the way I envisioned. So today I wanted to try a top-down approach.
I’m running this experiment on a fresh new project that targets developers. I’m going to start with a feature that removes some of the mundane tasks of development. Specifically, when I practice TDD in Java, I start out with writing a test class. In that class, I create an instance of the Class Under Test (CUT). Since the CUT doesn’t exist at this point in time, my code doesn’t compile. So I need to create the CUT to make it compile. In Java, that consists of a couple of actions that are pretty uninteresting, but that need to be done anyway. This takes away my focus from the test, so it would be kinda cool if it somehow could be automated.
In work mostly in Eclipse, and Eclipse has the notion of Quick Fixes. So that seems like a perfect fit. However, I don’t want my project code to be completely dependent on Eclipse, if only because independent code is easier to test.
So I start out with a top-down test that shows how all of this is accomplished:
public class FixesFactoryTest { @Test public void missingClassUnderTest() { FixesFactory fixesFactory = new FixesFactory(); Issues issues = new Issues().add(Issue .newProblem(new MissingType(new FullyQualifiedName("Bar"))) .at(new FileLocation( new Path("src/test/java/com/acme/foo/BarTest.java"), new FilePosition(new LineNumber(11), new ColumnNumber(5))))); Fixes fixes = fixesFactory.newInstance(issues); Assert.assertNotNull("Missing fixes", fixes); Assert.assertEquals("# Fixes", 1, fixes.size()); Fix fix = fixes.iterator().next(); Assert.assertNotNull("Missing fix", fix); Assert.assertEquals("Fix", CreateClassFix.class, fix.getClass()); CreateClassFix createClassFix = (CreateClassFix) fix; Assert.assertEquals("Name of new class", new FullyQualifiedName("com.acme.foo.Bar"), createClassFix.nameOfClass()); Assert.assertEquals("Path of new class", new Path("src/main/java/com/acme/foo/Bar.java"), createClassFix.pathOfClass()); } }
This test captures my intented design: a FixesFactory
gives Fixes
for Issues
, where an Issue
is a Problem
at a given Location
. This will usually be a FileLocation
, but I envision there could be problems between files as well, like a test class whose name doesn’t match the name of its CUT. For this particular issue, I expect one fix: to create the missing CUT at the right place.
I’m trying to follow the rules of Object Calistenics here, hence the classes like LineNumber
where one may have expected a simple int
. Partly because of that, I need a whole bunch of classes and methods before I can get this test to even compile. This feels awkward, because it’s too big a step for my taste. I want my green bar!
Obviously, I can’t make this pass with a few lines of code. So I add a @Ignore
to this test, and shift focus to one of the smaller classes. Let’s see, LineNumber
is a good candidate. I have no clue as to how I’ll be using this class, though. All I know at this point, is that it should be a value object:
public class LineNumberTest { @Test public void valueObject() { LineNumber lineNumber1a = new LineNumber(313); LineNumber lineNumber1b = new LineNumber(313); LineNumber lineNumber2 = new LineNumber(42); Assert.assertTrue("1a == 1b", lineNumber1a.equals(lineNumber1b)); Assert.assertFalse("1a == 2", lineNumber1a.equals(lineNumber2)); Assert.assertTrue("# 1a == 1b", lineNumber1a.hashCode() == lineNumber1b.hashCode()); Assert.assertFalse("# 1a == 2", lineNumber1a.hashCode() == lineNumber2.hashCode()); Assert.assertEquals("1a", "313", lineNumber1a.toString()); Assert.assertEquals("1b", "313", lineNumber1b.toString()); Assert.assertEquals("2", "42", lineNumber2.toString()); } }
This is very easy to implement in Eclipse: just select the Quick Fix to Assign Parameter To Field on the constructor’s single parameter and then select Generate hashCode() and equals()…:
public class LineNumber { private final int lineNumber; public LineNumber(int lineNumber) { this.lineNumber = lineNumber; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + lineNumber; return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } LineNumber other = (LineNumber) obj; if (lineNumber != other.lineNumber) { return false; } return true; } }
This is not the world’s most elegant code, so we’ll refactor this once we’re green. But first we need to add the trivial toString()
:
@Override public String toString() { return Integer.toString(lineNumber); }
And we’re green.
EclEmma tells me that some code in LineNumber.equals()
is not covered. I can easily fix that by removing the if
statements. But the remainder should clearly be refactored, and so should hashCode()
:
@Override public int hashCode() { return 31 + lineNumber; } @Override public boolean equals(Object object) { LineNumber other = (LineNumber) object; return lineNumber == other.lineNumber; }
The other classes are pretty straightforward as well. The only issue I ran into was a bug in EclEmma when I changed an empty class to an interface. But I can work around that by restarting Eclipse.
If you are interested to see where this project is going, feel free to take a look at SourceForge. Maybe you’d even like to join in!
Retrospective
So what does this exercise teach me? I noted earlier that it felt awkward to be writing a big test that I can’t get to green. But I now realize that I felt that way because I’ve trained myself to be thinking about getting to green quickly. After all, that was always the purpose of writing a test.
But it wasn’t this time. This time it was really about writing down the design. That part I usually did in my head, or on a piece of paper or whiteboard before I would write my first test. By writing the design down as a test, I’m making it more concrete than UML could ever hope to be. So that’s definitely a win from my perspective.
The other thing I noted was not so good: I set out to write a top-down test, yet I didn’t. I didn’t start at the bottom either, but somewhere in the middle. I was quick to dismiss the Eclipse part, because I wanted at least part of the code to be independent from Eclipse. Instead, I should have coded all of that up in a test. That would have forced me to consider whether I can actually make the design work in an Eclipse plug-in. So I guess I have to practice a bit more at this top-down TDD stuff…
2 thoughts on “Top-Down Test-Driven Development”