Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Unit Test Code

ArrayUtils.java - imports and start of class
/*
 * 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

ArrayUtilsTest.java - iteration 1
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

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


Iteration 2

ArrayUtilsTest.java - iteration 2
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

ArrayUtils.java - iteration 2
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;
    }
}


  • No labels

0 Comments

You are not logged in. Any changes you make will be marked as anonymous. You may want to Log In if you already have an account.