Test Driven Development

Testing State Versus Testing Identity

Defining the class to be Tested

public class Person {
    private String fName, lName, email;

    public Person(String fName, String lName, String email) {
        this.fName = fName;
        this.lName = lName;
        this.email = email;
    }
    // getters and setters omitted for brevity
}

Comparing Instances with Identical State

public class PersonTest {
    @Test
    public void testState() {
        // Given
        String firstName = "Leon";
        String lastName = "Hunter";
        String email = "leon@perscholas";
        
        // when
        Person person1 = new Person(firstName, lastName, email);
        Person person2 = new Person(firstName, lastName, email);
        
        //then
        Assert.assertEquals(person1, person2);
    }
}

Understanding Assert.assertEquals

Undserstanding .equals to Compare by Object-State

Understanding .equals to Compare by Object-State

public class PersonTest {
    @Test
    public void testState() {
        // Given
        String firstName = null;
        String lastName = "Hunter";
        String email = "leon@perscholas";
        
        // when
        Person person1 = new Person(firstName, lastName, email);
        Person person2 = new Person(firstName, lastName, email);
        
        //then
        Assert.assertEquals(person1, person2); // NullPointerException
    }
}

Undserstanding Objects.equals to Compare by Object-State

Understanding Assert.assertArrayEquals

Comparing Arrays with Identical Contents

Example 1

@Test
public void testArrayEquivalence() {
    Integer[] array1 = new Integer[]{7,1,5,2};
    Integer[] array2 = new Integer[]{1,5,2,7};

    Assert.assertArrayEquals(array1, array2);
}

Comparing Arrays with Identical Contents

Example 2

@Test
public void testArrayEquivalence() {
    Object object1 = new Object();
    Object object2 = new Object();
    Object object3 = new Object();
    Object object4 = new Object();

    Objcet[] array1 = new Object[]{object1, object2, object3, object4};
    Objcet[] array2 = new Object[]{object3, object4, object2, object1};

    Assert.assertArrayEquals(array1, array2);
}

Comparing Arrays with Non-Identical Contents

Example 1

@Test
public void testArrayEquivalence() {
    Object object1 = new Object();
    Object object2 = new Object();
    Object object3 = new Object();
    Object object4 = new Object();

    Objcet[] array1 = new Object[]{object1, object2, object3, object4};
    Objcet[] array2 = new Object[]{object1, object2, object3};

    Assert.assertArrayEquals(array1, array2);
}

Comparing Arrays with Non-Identical Contents

Example 2

@Test
public void testArrayEquivalence() {
    Object object1 = new Object();
    Object object2 = new Object();
    Object object3 = new Object();
    Object object4 = new Object();

    Objcet[] array1 = new Object[]{object1, object2, object3, object4};
    Objcet[] array2 = new Object[]{object1, object2, object3, object4, null};

    Assert.assertArrayEquals(array1, array2);
}