Just for kicks, some more sheep…

Here’s a quick test for the fence from my last post. My customer wants a fence which allows sheep to graze in a field, and prevents sheep from wandering outside of the field.

public class FenceTest extends TestCase {

    public void testShouldAllowSheepToGrazeInField() {
    
        Field field = new Field(3, 4);
        Sheep sheep = new Sheep();
    	
        Fence fence = new Fence(field);
    	
        boolean sheepGrazes = fence.sheepGrazes(sheep, 3, 3);
        assertTrue(sheepGrazes);
    }
    
    public void testShouldPreventSheepFromWanderingOutsideField() {
    	
        Field field = new Field(3, 4);
        Sheep sheep = new Sheep();
    	
        Fence fence = new Fence(field);
    	
        boolean sheepGrazes = fence.sheepGrazes(sheep, 3, 5);
        assertFalse(sheepGrazes); 	
    }
}

It’s easy to write code which implements this, so imagine that I’ve done it. Hurrah! Green bar. That was easy.

Now supposing that my customer wants to define the strength of my fence, so that it gives way if a really strong sheep comes along. (This is how things like Sims get started!) Here goes:


    public void testShouldAllowStrongSheepToBreakFenceAndWanderOutsideOfField() {

        Field field = new Field(3, 4)
        Sheep strongSheep = new Ram();

        Fence fence = new Fence(field);

        boolean strongSheepGrazes = fence.sheepGrazes(strongSheep, 3, 5);
        assertTrue(strongSheepGrazes);
    }

Another easily implemented test. Bang! Green bar. The customer is very happy with the way in which the Ram can escape the clutches of the fence and graze beyond the boundary of the field.

But wait! My pair has spotted something I missed. He speaks briefly to the customer about the definition of ‘break the fence’, then writes this excellent failing test:

    public void testShouldAllowAnySheepToWanderOnceFenceBroken() {

        Field field = new Field(3, 4)
        Sheep weakSheep = new Lamb();
        Sheep strongSheep = new Ram();

        Fence fence = new Fence(field);

        fence.sheepGrazes(strongSheep, 3, 5); // result covered by other test

        boolean weakSheepGrazes = fence.sheepGrazes(weakSheep, 6, 6);
        assertTrue(weakSheepGrazes);
    }

So now we implement a “broken” state in the fence, to allow sheep to graze. We know that the fence is still whole when sheep are originally put into it, because the second test still passes. We should probably rename the second test to testShouldPreventSheepFromWanderingOutsideFieldWhenUnbroken. Now no one will misinterpret the “documentation” provided by the test names, and the code will ensure that anyone who, say, puts Horses which can leap over the fence into the same field, doesn’t accidentally cause the Rams to start leaping over the fences too.

(In the real world I would mock the sheep out so that the fence’s behaviour was independent of the sheep. But you get the picture.)

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s