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

JUnit Testing Extras

There are many features in JUnit. You can learn quite a bit by going to Google.com and entering the keywords:

JUnit tutorials

Here is a link that I found useful for getting started:

https://courses.cs.washington.edu/courses/cse143/11wi/eclipse-tutorial/junit.shtml

JUnit is another topic where you can find books (see amazon.com) that cover the topic.

In this lecture I'm going to go over some of the extras that you might want to know about.



import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import fraction.Fraction;

public class TestFoolingAround {


    //************************************************
    // Playing around with other JUnit options

    @Test
    public void testOther(){
        // Wanted to see whether an empty test is considered a pass or fail: answer is pass
    }

    @Test
    public void testSomethingElse(){
        fail("Something Else: Unimplemented yet");
    }

    @Test
    public void testAddTryOther() {
        // Trying out assertTrue, assertFalse, assertSame, assertNotSame, assertNotNull, and assertNull

        Fraction f1 = new Fraction(1,2);
        Fraction f2 = new Fraction (1,4);
        Fraction fadd = f1.add(f2);
        assertTrue("add test ", fadd.equals(new Fraction(3,4)));
        assertFalse("add test ", fadd.equals(new Fraction(1,4)));
        Fraction f3 = new Fraction(3,4);
        Fraction f4=null;

        // assertSame uses == instead of equals for comparison whereas assertEquals uses the equals(Object) method

        assertSame("same  ", f3, f3);
        assertNotSame("not same ", f3, new Fraction(3,4));
        assertNotNull("not null ", f3);
        assertNull("null ", f4);

    }
    // Checking to see that the proper exception happens

    @Test(expected = ArithmeticException.class)
    public void createException(){
        int z, x=10, y=0;
        System.out.println("About to get a divide by 0 exception");
        z = x/y; // Expecting an ArithmeticException
        System.out.println("z="+z);
    }

    // Want to make sure a certain test runs in a give amount of time.  If it exceeds that then we fail

    @Test(timeout=100)
    public void testTakesTooLong()
    {
        // infinite loop
        while(true);
    }


    // Sometimes we want to do a test in a temporary folder that goes away after the test
    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test
    public void testUsingTempFolder() throws IOException {
        File createdFolder = folder.newFolder("newfolder");
        System.out.println("Temp folder= "+createdFolder.getPath());
        File createdFile = folder.newFile("myfilefile.txt");
        assertTrue(createdFile.exists());
// folder automatically gets cleaned up
    }

    // This will execute before our testing starts.  This is good for one time setup things.
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("setUpBeforeClass: At the beginning of our testing");
    }

    // This will execute after testing is completed.  This is where you can clean up and free up global resources
    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("tearDownAfterClass: At the end of our testing");
    }

    // This happens before each test
    @Before
    public void setUp() throws Exception {
        System.out.println("setUp: before each individual test");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("tearDown: after each individual test");
    }


}


Java Code Coverage:

http://www.eclemma.org - an Eclipse pluggin for testing coverage

Install: goto Eclipse help/Eclipse Marketplace

Put in a search for EclEmma and install the software.

Instead of using "run-as" you use "coverage-as" to run your application

Last Updated: June 9, 2015 8:11 PM