CS490M

Lab 1

Introduction to JUnit

 

Deadline: Friday September 22, 2006  11:59 PM

 

Overview

JUnit is an open source regression testing framework for Java, that lets you organize unit tests in such a way that they are easy to run automatically.  JUnit provides immediate feedback through a text or graphical user interface.  JUnit and its related documentation is available at http://www.junit.org/index.htm.

 

In this lab, you will be provided the source code for a simple class called Triangle.   Triangle stores the length of the three sides of a triangle (a, b, c) and has only one method called TriangleType.

 

TriangleType(a,b,c) should return the following values:

 

0 if the triangle is equilateral (a=b=c)

1 if the triangle is isosceles (two sides have equal length)

2 if the triangle is scalene (all sides have different length)

3 if the input is not a valid triangle (for example, a triangle with a=10, b=1, c=2 or a=0, b=4, c=5)

4 if any of the lengths is negative

 

Your goals in this lab are the following:

1)      Write unit tests for the provided method TriangleType using JUnit. 

2)      Fix the bugs that you find while running your tests.  The provided method has some intentional bugs, so that you can exercise your testing skills.  At the same time, the method is so short, that it should be easy to fix.

 

Lab Exercise

The necessary software to complete this lab exercise is available in the computers named PORT,  SCOTCH, WHISKEY, MEZCAL in CS175 (old building).

 

Follow these steps to complete the lab:

  1. Download the class that you will be testing from here.

 

  1. To get started, a simple testcase with the necessary fixture is provided to you here
Download the JUnit-Lab.zip and extract it.   The ZIP file contains the files Triangle.java and TriangleTest.java

 

 

  1. Read the following quick start guide

 

http://junit.sourceforge.net/doc/cookbook/cookbook.htm

 

  1. Read the following article, which explains the differences (and similarities) between older version of JUnit and the newest version: JUnit 4, which you will be using in this lab.

 

http://www.instrumentalservices.com/content/view/45/52/#_Old_JUnit_revisited

 

It is important that you understand how the testing fixtures are implemented in the older version, since a great amount of the documentation for JUnit found on the Web refers to the old version. You should be able to: (a) recognize in which version of JUnit sample code is written, (b) write equivalent code for the new version of JUnit, given code written in the old version

 

  1. Notice how JUnit is integrated (via the TriangleTest class) with the Triangle class:

 

a)      Import the following:

 

import org.junit.Test;

import static org.junit.Assert.*;

import junit.framework.JUnit4TestAdapter;

 

b)      Create a new Testing Class, in this case TriangleTest (in older versions you had to extend from TestClass)

c)      Implement your tests and annotate them with @org.junit.Test (in older versions all testing methods had to start with the word “test”)

For the tests you can choose among the following assertions:

·                     assertEquals

·                     assertTrue

·                     assertFalse

·                     assertNull

·                     assertNotNull

·                     assertSame

·                     assertNotSame

·                     fail

d)  Create a Test Suite that will run all the tests in TriangleTest automatically.

e)   Optionally you can include code that is always executed before and after your tests (for initializations, cleanup, etc.)

 

  1. Write the following suite of testcases:

 

a) Tests for valid triangles (3)

b) Tests for invalid triangles, with combinations of zero-values (6)

c) Tests for invalid triangles, with no zero values (3)

d) Tests for negative lengths (3)

 

Notice that a sample test called equilateral is provided in the sample code.

7.      After writing your tests, launch the Windows Command Prompt, compile your files using javac) and run JUnit in text mode with the following command:

java org.junit.runner.JUnitCore TriangleTest

Save a copy of the output to a file called originalTests.txt

  1. Modify the provided code as needed, as it has some (intentional) bugs.  Verify that your tests are run by JUnit, that the modified code passes all your tests and that TriangleType performs according to the specifications. 

 

When a test fails, JUnit will tell you:

a) The name of the failed test and also its class

b) A message, depending on the assert method used of the form

expected:0 <value1> but was:<value2>

c) A summary of how many tests were run and how many failed.

 

  1. Run your tests again and save a copy of the new output to a file called newTests.txt  (needless to say, all tests should pass, the interface should say “OK”.)  When you use JUnit’s GUI a red bar tells you that tests failed, while a green one indicates that they were successful.

 

Turnin

When you are satisfied that your program works correctly ( or you run out of time ), please do the following.

  1. Create a directory named lab1
  2. Copy all program and text files to the directory created in step 1.  You should include the following files:

·        Your testcases

·        The modified Triangle class

·        originalTests.txt output

·        newTests.txt output

  1. Create a zip file of the directory lab1 with the files you want to submit.
    1. Send the zip file to jrramos@cs.purdue.edu before the deadline.