Wednesday, July 31, 2013

Test Driven Development example - Part 4 (Simple Unit Testing for Java with NetBeans)

Lets code the all tests in the to do list. (this should be done one by, for the tutorial i used it just to show how to do it)
The FibonacciTest.java file will be look like

 
 
package tdd;

import org.junit.Test;
import static org.junit.Assert.*;
 
public class FibonacciTest {

    @Test
    public void testGetFibonacci_0() {
        int x = 0;
        Fibonacci instance = new Fibonacci();
        int expResult = 0;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }

    @Test
    public void testGetFibonacci_1() {
        int x = 1;
        Fibonacci instance = new Fibonacci();
        int expResult = 1;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }

    @Test
    public void testGetFibonacci_2() {
        int x = 2;
        Fibonacci instance = new Fibonacci();
        int expResult = 1;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }

    @Test
    public void testGetFibonacci_3() {
        int x = 3;
        Fibonacci instance = new Fibonacci();
        int expResult = 2;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }

    @Test
    public void testGetFibonacci_4() {
        int x = 4;
        Fibonacci instance = new Fibonacci();
        int expResult = 3;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }

    @Test
    public void testGetFibonacci_5() {
        int x = 5;
        Fibonacci instance = new Fibonacci();
        int expResult = 5;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }

    @Test
    public void testGetFibonacci_6() {
        int x = 6;
        Fibonacci instance = new Fibonacci();
        int expResult = 8;
        int result = instance.getFibonacci(x);
        assertEquals(expResult, result);
    }
}


Just run the tests and see before progress. :)
we added 4 tests, they will fail.

lets pass the 4th test.
Edit the Fibonacci.java like below to produce the first 4 Fibonacci number correctly
 
package tdd;

public class Fibonacci {

    public int getFibonacci(int x) {
        int result = 0;
        if (x == 1 || x == 2) {
            result = 1;
        } else if (x==3) {
            result = 2;
        }
        return result;
    }
}

The result will be

Lets write in the typical way to pass all the test correctly.
 
package tdd;

public class Fibonacci {

    public int getFibonacci(int x) {
        int result = 0;
        if (x == 1 || x == 2) {
            result = 1;
        } else if (x == 3) {
            result = 2;
        } else if (x == 4) {
            result = 3;
        } else if (x == 5) {
            result = 5;
        } else if (x == 6) {
            result = 8;
        }
        return result;
    }
}


Then all the tests will be passed. We can do this type of code for every other input as well. But we know that it cannot be done manually :) so we have to seek for a pattern in this input outputs.
And there is a pattern. The nth Fibonacci number is equal to the
(n-1)th Fibonacci number + (n-2)th Fibonacci number
Lets modify the code to produce every Fibonacci number correctly
 
package tdd;

public class Fibonacci {

    public int getFibonacci(int x) {
        int fibNumber = 1;
        if (x == 0) {
            fibNumber = 0;
        } else if (x <= 2) {
            fibNumber = 1;
        } else {
            fibNumber = getFibonacci(x - 1) + getFibonacci(x - 2);
        }
        return fibNumber;
    }
}


Lets check the correctness of the program by some random test cases. :)
Lets add this test to check some other known Fibonacci numbers.
 
 @Test
    public void testFibonacci() {
        Fibonacci fibonacci = new Fibonacci();

        int cases[][] = {{7, 13}, {8, 21}, {9, 34}, {10, 55}, {11, 89}, {18, 2584}, {19, 4181}};
        for (int i = 0; i < cases.length; i++) {
            assertEquals(cases[i][1], fibonacci.getFibonacci(cases[i][0]));
        }

    }


Looks like our code pass those random cases as well. ok now we can assume that our code is working :)

0 comments:

Post a Comment