Test Naming Convention

Convention

I’m a big fan of the following test naming convention. Note the underscores.

  • test<NameOfMethodUnderTest>_<Condition1>_<Condition2>…_<ConditionN>

Example

I would would write the following tests for the Person class.

package example;

import junit.framework.TestCase;

public class TestPerson extends TestCase {

    public void testGetDefaultName_NullNames() {
        Person person = new Person(null, true, true);
        assertEquals("", person.getDefaultName());
    }

    public void testGetDefaultName_EmptyNames() {
        Person person = new Person(new String[] {}, true, true);
        assertEquals("", person.getDefaultName());
    }

    public void testGetDefaultName() {
        Person person = new Person(new String[] { "Smith" }, true, true);
        assertEquals("Smith", person.getDefaultName());
    }

    public void testGetDefaultSalutation_NullNames() {
        Person person = new Person(null, true, true);
        assertEquals("", person.getDefaultSalutation());
    }

    public void testGetDefaultSalutation_EmptyNames() {
        Person person = new Person(new String[] {}, true, true);
        assertEquals("", person.getDefaultSalutation());
    }

    public void testGetDefaultSalutation_Adult_Female() {
        Person person = new Person(new String[] { "Smith" }, true, true);
        assertEquals("Ms. Smith", person.getDefaultSalutation());
    }

    public void testGetDefaultSalutation_Adult_Male() {
        Person person = new Person(new String[] { "Smith" }, true, false);
        assertEquals("Mr. Smith", person.getDefaultSalutation());
    }

    public void testGetDefaultSalutation_Child_Female() {
        Person person = new Person(new String[] { "Smith" }, false, true);
        assertEquals("Miss Smith", person.getDefaultSalutation());
    }

    public void testGetDefaultSalutation_Child_Male() {
        Person person = new Person(new String[] { "Smith" }, false, false);
        assertEquals("Master Smith", person.getDefaultSalutation());
    }
}
package example;

public class Person {

    private static final int DEFAULT_NAME_INDEX = 0;
    private static final String SALUTATION_CHILD_MALE = "Master";
    private static final String SALUTATION_CHILD_FEMALE = "Miss";
    private static final String SALUTATION_ADULT_MALE = "Mr.";
    private static final String SALUTATION_ADULT_FEMALE = "Ms.";
    private String[] names;
    private boolean female;
    private boolean adult;

    public Person(String[] names, boolean adult, boolean female) {
        this.names = names;
        this.adult = adult;
        this.female = female;
    }

    public String getDefaultName() {
        if (this.names != null && this.names.length > 0) {
            return this.names[DEFAULT_NAME_INDEX];
        } else {
            return "";
        }
    }

    public String getDefaultSalutation() {
        String defaultName = getDefaultName();
        if (!"".equals(defaultName)) {
            if (this.adult && this.female) {
                return SALUTATION_ADULT_FEMALE + " " + defaultName;
            } else if (this.adult && !this.female) {
                return SALUTATION_ADULT_MALE + " " + defaultName;
            } else if (!this.adult && this.female) {
                return SALUTATION_CHILD_FEMALE + " " + defaultName;
            } else {
                return SALUTATION_CHILD_MALE + " " + defaultName;
            }
        }
        return defaultName;
    }
}

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply