Title: JUnit Testing
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

JUnit Testing

Software Testing is an important area that we often don't spend much time.

It is valuable to have a "pre-canned" set of tests for programs that you develop. It will allow you to:

- if you just made a small change and you want to see if you broke everything
- Gives you a way to systematically test all of the paths in your code
- It makes testing more of systematic science instead of a hap-hazard undisciplined exercise.
- Your future employer will be impressed with your diligence to program correctness
- If your program takes user imputs, it provides a quick way to enter in all of the possible user inputs

JUnit is a capability that will run in Eclipse and give you features that facilitate testing your code. The documentation is outside of the normal Java API, but can be found in the following URL:

http://junit.org/javadoc/latest/

To demonstrate this I have put together some tests for the Fraction class solution that I use in cps161. I have changed the class a bit to facilitate using JUnit on it. So the program that we will be demonstrating JUnit on is:

Fraction.java

To create a JUnit test module for Fraction, you do New/JUnit Test Case.

FractionTest.java ( a JUnit test case example for Fraction)



import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Scanner;


import org.junit.Test;

import fraction.Fraction;

public class FractionTest {



    @Test
    public void testFractionIntInt() {
        assertEquals(new Fraction(10,20), new Fraction(1,2));
    }

    @Test
    public void testAdd() {
        Fraction f1 = new Fraction(1,2);
        Fraction f2 = new Fraction (1,4);
        assertEquals("add test ", new Fraction(3,4), f1.add(f2));

    }

    @Test
    public void testSubtract() {
        Fraction f1 = new Fraction(10,20);
        Fraction f2 = new Fraction (2,8);
        assertEquals(new Fraction(1,4), f1.subtract(f2));
    }


    @Test
    public void testMultiply() {
        Fraction f1 = new Fraction(10,20);
        Fraction f2 = new Fraction (2,8);
        assertEquals(new Fraction(1,8), f1.multiply(f2));
    }

    @Test
    public void testDivide() {
        Fraction f1 = new Fraction(10,20);
        Fraction f2 = new Fraction (2,8);
        assertEquals(new Fraction(2,1), f1.divide(f2));
    }

    @Test
    public void testEqualsFraction() {
        Fraction f1=new Fraction(30,60);
        assertTrue(f1.equals(new Fraction(1,2)));
    }
    private void testOneOperation(String input, String expectedOutput, boolean debug)
    {
        OutputStream myOutputStream = new ByteArrayOutputStream();

        Scanner scan= new Scanner(input);
        PrintStream ps = new PrintStream(myOutputStream);
        Fraction.do_one_operation(scan, ps);


        if (debug)
        {
            System.out.println("********");
            System.out.println(myOutputStream.toString());
            System.out.println("********");
        }

        assertEquals(expectedOutput, myOutputStream.toString());

        scan.close();
        ps.close();
    }

    @Test
    public void testInput1() {
        String input = "+" + System.lineSeparator() + "1/2" + System.lineSeparator() +
                             "1/2" + System.lineSeparator() ;

        String expectedOutput = System.lineSeparator() + "Enter operation: + - * / q (q ==> quit) : Enter Fraction 1: Enter Fraction 2: f1 = 1/2" +
                System.lineSeparator() + "f2 = 1/2" +
                System.lineSeparator() + "f1 and f2 are equal" + System.lineSeparator() +
                "f1+f2=1/1" + System.lineSeparator() ;

        testOneOperation(input, expectedOutput, false);

    }

    @Test
    public void testInput2() {
        String input = "-" + System.lineSeparator() + "1/2" + System.lineSeparator() +
                             "1/4"+ System.lineSeparator();

        String expectedOutput = System.lineSeparator() + "Enter operation: + - * / q (q ==> quit) : Enter Fraction 1: Enter Fraction 2: f1 = 1/2" +
                System.lineSeparator() + "f2 = 1/4" + System.lineSeparator() +
                "f1 and f2 are not equal" +System.lineSeparator() +
                "f1-f2=1/4" + System.lineSeparator();

        testOneOperation(input, expectedOutput,false);

    }

    @Test
    public void testInput3() {
        String input = "*" + System.lineSeparator() + "3/4" + System.lineSeparator() +
                             "1/2" + System.lineSeparator()  ;

        String expectedOutput = System.lineSeparator() + "Enter operation: + - * / q (q ==> quit) : Enter Fraction 1: Enter Fraction 2: f1 = 3/4" +
                System.lineSeparator() + "f2 = 1/2" + System.lineSeparator() +
                "f1 and f2 are not equal" + System.lineSeparator() +
                "f1*f2=3/8" + System.lineSeparator() ;

        testOneOperation(input, expectedOutput,false);

    }

    @Test
    public void testInput4() {
        String input = "/" + System.lineSeparator() + "1/2"+ System.lineSeparator() +
                             "1/2" + System.lineSeparator();

        String expectedOutput = System.lineSeparator()+ "Enter operation: + - * / q (q ==> quit) : Enter Fraction 1: Enter Fraction 2: f1 = 1/2" +
                System.lineSeparator() + "f2 = 1/2" + System.lineSeparator() +
                "f1 and f2 are equal" + System.lineSeparator() +
                "f1/f2=1/1" + System.lineSeparator();

        testOneOperation(input, expectedOutput,false);

    }

}



Last Updated: June 9, 2015 2:27 PM