Joe Walnes
  Blog



Recent Entries

Creative uses of Hamcrest matchers

Hamcrest 1.1 released

Testing on the Toilet

Building testable AJAX apps (Does my button look big in this?)

QDox is back - 1.6 released

Java and .NET RESTful interoperability with XStream

I've joined Google

OSCon: SiteMesh, SiteMesh, SiteMesh, SiteMesh

Flexible JUnit assertions with assertThat()

SiteMesh and Content Management @ O'Reilly OpenSource Conference

XStream 1.1.2 released. Java 5 Enums, JavaBeans, field aliasing, StAX, and more...

VB.Net is the bestest

XStream 1.1.1 released

Accessing generic type information at runtime

XStream 1.1 released

JUnit tip: Setting the default timezone with a TestDecorator

XStream: how to serialize objects to non XML formats

How my backflip went...

Backflippin' in 4 hours.

Is 100% test coverage a BAD thing?

Looking back at the SiteMesh HTML parser

The road ahead for SiteMesh 3

Joe's Backflipping for Autistic Research - time is nearly up...

SiteMesh 2.2 Released

Advanced SiteMesh

More... [RSS | RDF]

About Joe Walnes

I am a software engineer for Google, based in London.

Open Source

WebStuff (coming soon)

XStream

ActiveMQ

SiteMesh

QDox

nMock

jMock

Pico Container

Nano Container

OpenSymphony

Squiggle

MockDoclet

MockObjects

Jelly

Groovy

PatternStitcher

XJB

Books

Java Open Source Programming, Wiley JSP Site Design, Wrox

Talks

Mock Roles, not Objects
October 26 2004, Vancouver, Canada. OOPSLA'04

Personal Development Practices Map
June 24 2004, Salt Lake City, Utah. Agile Development Conference

SiteMesh.NET and ASP.NET MasterPages
May 20 2004, Bangalore, India. Bangalore .NET User Group

Mock Objects: Driving Top Down Development
March 29 2004, St Neots, UK. OT2004

Mock Objects
December 2 2003, London, UK. XP Day 3


Design by contract: testing implementations of interfaces

Here's a nice way of associating contracts with interfaces and testing the implementations conform.

A sample interface:

interface CheeseMaker {
    int getCheeseCount();
    void addCheese(Cheese cheese);
}

With the contracts:

  • there should be zero cheeses on creation.
  • adding a cheese should increment the count.
  • unless the cheese is a duplicate.
  • when adding a cheese, the cheese cannot be null.

You can create an abstract unit test for this interface that tests these contracts. The only thing it doesn't do is provide an implementation - instead it has an abstract factory method.

public abstract class CheeseMakerTest extends TestCase {

    // abstract factory method
    protected abstract CheeseMaker createCheeseMaker(); 

    public void testZeroCheesesOnCreation() {
        CheeseMaker cheeseMaker = createCheeseMaker();
        assertEquals(0, cheeseMaker.getCheeseCount());
    }

    public void testAddingACheeseIncrementsCount() {
        CheeseMaker cheeseMaker = createCheeseMaker();
        cheeseMaker.addCheese(new Cheese("Cheddar"));
        cheeseMaker.addCheese(new Cheese("Wensleydale"));
        assertEquals(2, cheeseMaker.getCheeseCount());
    }

    public void testDuplicateCheesesDoNotIncrementCount() {
        CheeseMaker cheeseMaker = createCheeseMaker();
        cheeseMaker.addCheese(new Cheese("Cheddar"));
        cheeseMaker.addCheese(new Cheese("Cheddar"));
        assertEquals(1, cheeseMaker.getCheeseCount());
    }

    public void testNullCheeseCausesIllegalArgumentException() {
        CheeseMaker cheeseMaker = createCheeseMaker();
        try {
            cheeseMaker.addCheese(null);
            fail("expected exception");
        } catch (IllegalArgumentException e) {} // good
    }

}

Now, every time you create an implementation of CheeseMaker, the test should extend CheeseMakerTest and you inherit the contract tests for free.

For example:

public class BigCheeseMaker implements CheeseMaker {
    // ... ommited for sanity
}

public class BigCheeseMakerTest extends CheeseMakerTest {

    // factory method implementation
    protected CheeseMaker createCheeseMaker() { 
        return new BigCheeseMaker();
    }
    
    // ... any additional tests go here

}

It's important to note that this tests the contract but does not enforce them. It's very flexible and there are very few contracts (if any at all) that couldn't be expressed in a unit-test.

This helps defensive development with the added safety of unit-tests.

As a bonus, you can use TestDox to generate documentation for interfaces (much more useful than implementations), like so:

CheeseMaker

  • Zero cheeses on creation.
  • Adding a cheese increments count.
  • Duplicate cheeses do not increment count.
  • Null cheese causes illegal argument exception.

Comments

オンラインカジノ

http://www.motherhensden.com/ja/

Casino En Ligne

[url=http://www.motherhensden.com/fr/]Casino En Ligne[/url]

On Line Kasino

http://www.motherhensden.com/de/

Casino En línea

http://www.motherhensden.com/es/

buy viagra

Good quality of site!
buy viagra

reneguido

DeLonghi Fresh Zone Air Purifier

sanpedro5

DeLonghi Fresh Zone Air Purifier

dlidcimp

Hello!
Thanx thanx thanx for such great job done above your website!

DS2_sp_3

oxokvobl

Hello!
Not much on my mind today. What can I say? I've just been sitting around waiting for something to happen. But i found this site and became happy!

DS2_sp_1

veibuehl

Hello!
You're website looks very good, it was a pleasure to be on you're. Keep on the good work :-)

DS2_sp_2

rondehunt

hemorrhoids cream [URL=www.s190000990.onlinehome.us/]hemorrhoids cream[/URL] hemorrhoids creams [URL=www.s190000990.onlinehome.us/]hemorrhoids creams[/URL]

lissarandol

hemorrhoid pain relief [URL=www.s190000990.onlinehome.us/]hemorrhoid pain relief[/URL] hemorrhoid ointment [URL=www.s190000990.onlinehome.us/]hemorrhoid ointment[/URL]

copadete

hemorrhoids relief [URL=www.s190000990.onlinehome.us/]hemorrhoids relief[/URL] treat hemorrhoids [URL=www.s190000990.onlinehome.us/]treat hemorrhoids[/URL]

pedroescamoso8

hemorrhoid treatments [URL=www.s190000990.onlinehome.us/]hemorrhoid treatments[/URL] hemorrhoid cures [URL=www.s190000990.onlinehome.us/]hemorrhoid cures[/URL]

rijkdgooier

hemroids [URL=www.s190000990.onlinehome.us/]hemroids[/URL] natural hemorrhoids treatment [URL=www.s190000990.onlinehome.us/]natural hemorrhoids treatment[/URL]

mariahernandez

hemorrhoids treatments [URL=www.s190000990.onlinehome.us/]hemorrhoids treatments[/URL] hemorrhoid treatment [URL=www.s190000990.onlinehome.us/]hemorrhoid treatment[/URL]

freddyfish_54

hemorrhoid relief [URL=www.s190000990.onlinehome.us/]hemorrhoid relief[/URL] hemorrhoid natural treatment [URL=www.s190000990.onlinehome.us/]hemorrhoid natural treatment[/URL]

renesluiters_NOSPAM_

hemorrhoids cream [URL=www.s190000990.onlinehome.us/]hemorrhoids cream[/URL] hemorrhoids creams [URL=www.s190000990.onlinehome.us/]hemorrhoids creams[/URL]

gueiaplr jcphuvmts

fhqdobgyz dslybehrj anvc ofxdaz hmxany wkstoylum uoliktgxa

gueiaplr jcphuvmts

fhqdobgyz dslybehrj anvc ofxdaz hmxany wkstoylum uoliktgxa

lenerdfloris

menopause natural remedies [URL=www.s190005253.onlinehome.us/]menopause natural remedies[/URL] menopause pms [URL=www.s190005253.onlinehome.us/]menopause pms[/URL]

morrismony00

menopause help [URL=www.s190005253.onlinehome.us/]menopause help[/URL] menopause herbal [URL=www.s190005253.onlinehome.us/]menopause herbal[/URL]

mariahernandez

menapause [URL=www.s190005253.onlinehome.us/]menapause[/URL] menapause natural treatment [URL=www.s190005253.onlinehome.us/]menapause natural treatment[/URL]

milton_99

menopause patch [URL=www.s190005253.onlinehome.us/]menopause patch[/URL] menopause org [URL=www.s190005253.onlinehome.us/]menopause org[/URL]

Name:
Email:
URL:

ThoughtBloggers

Martin Fowler

Dan North

Aslak Hellesoy

Darren Hobbs

Geoff Oliphant

Mike Roberts

Chris Stevenson

Jon Tirsen

Loads More...

Agile Bloggers

Ken Arnold

Ward Cunningham

Brian Marick

Robert Martin

Bret Pettichord

Java Bloggers

Ara Abrahamian

Mike Cannon-Brookes

Vincent Massol

Bob McWhirter

Rickard Oberg

Joseph Ottinger

James Strachan

Hani Suleiman

Communities

eXtreme Tuesday Club (XTC)

Thursday GeekSpeek

ThoughtWorks GeekNight

London Java Meetup

The Codehaus

[RSS | RDF]
© 2001-2004, Joe Walnes

Powered by SiteMesh and Moveable Type.