Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Unit Test Code

Code Block
languagejava
titleArrayUtils.java - imports and start of class
linenumberstrue
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.celestial.tdd.demo.v1;

import static org.hamcrest.CoreMatchers.is;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

/**
 *
 * @author Selvyn
 */
public class ArrayUtilsTest
{
   ...

Iteration 1

Code Block
languagejava
titleArrayUtilsTest.java - iteration 1
linenumberstrue
public class ArrayUtilsTest
{
    @Test
    public void find_highest_in_array_of_one()
    {
        // Arrange
        int array[] =
        {
            10
        };
        ArrayUtils arrayUtils = new ArrayUtils();

        // Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(10));
    }
}

CUT

Code Block
languagejava
titleArrayUtils .java - iteration 1
linenumberstrue
class ArrayUtils
{
    // Version 1 for find_highest_in_array_of_one()
    int findHighest(int[] array)
    {
        return array[0];
    }
}


Iteration 2

Code Block
languagejava
titleArrayUtilsTest.java - iteration 2
linenumberstrue
public class ArrayUtilsTest
{
    @Test
    public void find_highest_in_array_of_one()
    {
        // Arrange
        int array[] =
        {
            10
        };
        ArrayUtils arrayUtils = new ArrayUtils();

        // Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(10));
    }

    @Test
    public void find_highest_in_array_of_two_pre_ordered()
    {
        // Arrange
        int array[] =
        {
            20, 10
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 20;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }

    @Test
    public void find_highest_in_array_of_two_ascending()
    {
        // Arrange
        int array[] =
        {
            10, 20
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 20;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }
}

CUT

Code Block
languagejava
titleArrayUtils.java - iteration 2
linenumberstrue
class ArrayUtils
{
    // Version 2 for find_highest_in_array_of_two_...()
    public  int findHighest(int array[])
    {
        int highestSoFar = array[0];
        
        if( array.length > 1 && array[1] > highestSoFar )
            highestSoFar = array[1];
        
        return highestSoFar;
    }
}


Iteration 3

Code Block
languagejava
titleArrayUtilsTest.java - iteration 3
linenumberstrue
public class ArrayUtilsTest
{
    public ArrayUtilsTest()
    {
    }

    @Before
    public void setUp()
    {
    }

    @Test
    public void find_highest_in_array_of_one()
    {
        // Arrange
        int array[] =
        {
            10
        };
        ArrayUtils arrayUtils = new ArrayUtils();

        // Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(10));
    }

    @Test
    public void find_highest_in_array_of_two_pre_ordered()
    {
        // Arrange
        int array[] =
        {
            20, 10
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 20;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }

    @Test
    public void find_highest_in_array_of_two_ascending()
    {
        // Arrange
        int array[] =
        {
            10, 20
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 20;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }

    @Test
    public void find_highest_in_array_of_any_number()
    {
        // Arrange
        int array[] =
        {
            10, 5, 30
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 30;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }
}

CUT 

Code Block
languagejava
titleArrayUtils.java - iteration 3
linenumberstrue
class ArrayUtils
{
    // Version 3 for find_highest_in_array_of_any_number()
    public  int findHighest(int array[])
    {
        int highestSoFar = Integer.MIN_VALUE;  // it could be argued that it should be array[0], to restrict numbers to within that which is passed in
        
        for (int i = 0; i < array.length; i++) 
        {
            if (array[i] > highestSoFar) 
		highestSoFar = array[i];
        }
        
        return highestSoFar;
    }
}


Final Version - handles exceptions

Code Block
languagejava
titleArrayUtilsTest.java -final iteration
linenumberstrue
public class ArrayUtilsTest
{
    @Rule
    public  final   ExpectedException exception = ExpectedException.none();
    
    @Test
    public void find_highest_in_empty_array() throws EmptyArrayException
    {
        // Arrange
        int array[] = {};
        ArrayUtils arrayUtils = new ArrayUtils();
        exception.expect(EmptyArrayException.class);

        // Act
        int result = arrayUtils.findHighest(array);
    }

    @Test
    public void find_highest_in_array_of_one() throws EmptyArrayException
    {
        // Arrange
        int array[] =
        {
            10
        };
        ArrayUtils arrayUtils = new ArrayUtils();

        // Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(10));
    }

    @Test
    public void find_highest_in_array_of_two_pre_ordered() throws EmptyArrayException
    {
        // Arrange
        int array[] =
        {
            20, 10
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 20;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }

    @Test
    public void find_highest_in_array_of_two_ascending() throws EmptyArrayException
    {
        // Arrange
        int array[] =
        {
            10, 20
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 20;

        //Act
        int result = arrayUtils.findHighest(array);

        // Assert
        assertThat(result, is(expectedResult));
    }

    @Test
    public void find_highest_in_array_of_any_number() throws EmptyArrayException
    {
        // Arrange
        int array[] =
        {
            10, 5, 30
        };
        ArrayUtils arrayUtils = new ArrayUtils();
        int expectedResult = 30;

        //Act
            int result = arrayUtils.findHighest(array);

            // Assert
            assertThat(result, is(expectedResult));
    }
}

...