Test Driven Development - Shortcomings of God-Object Design

Lecture Overview

God-Object Antipatterns

Shortcomings of God-Object Design

Untestable God-Object Design Example

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String[] invalidCharArray = "!@#$%^&*()".split("");
    List<String> invalidChars =Arrays.asList(invalidCharArray);

    System.out.println("Create a username:");
    String userName = scanner.nextLine();
    String[] userNameCharacterArray = userName.split("");
    for(String currentCharacter : userNameCharacterArray) {
        if(invalidChars.contains(currentCharacter)) {
            System.out.println("Invalid username!");
            return;
        }
    }
    System.out.println("Username is valid!");
}

Converting from God-Object Design to Divide-And-Conquer Design

Part 1

public static Boolean isValid(String userName) {
    String[] invalidCharArray = "!@#$%^&*()".split("");
    List<String> invalidChars = Arrays.asList(invalidCharArray);
    String[] userNameCharacterArray = userName.split("");
    for (String currentCharacter : userNameCharacterArray) {
        if (invalidChars.contains(currentCharacter)) {
            System.out.println("Invalid username!");
            return false;
        }
    }
}

Converting from God-Object Design to Divide-And-Conquer Design

Part 2

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Create a username:");
    String userName = scanner.nextLine();
    if (isValid(userName)) {
        System.out.println("Username is valid!");
    }
}

Positive and Negative Testing

Positive Testing a Divide-And-Conquer Design

public class MainApplicationTest {
    @Test
    public void test1() {
        Assert.assertTrue(MainApplication.isValid("fred"));
    }

    @Test
    public void test2() {
        Assert.assertTrue(MainApplication.isValid("John"));
    }

    @Test
    public void test3() {
        Assert.assertTrue(MainApplication.isValid("George"));
    }
}

Negative Testing a Divide-And-Conquer Design

public class MainApplicationTest {
    @Test
    public void test1() {
        Assert.assertFalse(MainApplication.isValid("Freddy!"));
    }

    @Test
    public void test2() {
        Assert.assertFalse(MainApplication.isValid("@perscholas"));
    }

    @Test
    public void test3() {
        Assert.assertFalse(MainApplication.isValid("$500.50"));
    }
}

TDD Behavior Enforcement