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:
Download the JUnit-Lab.zip and extract it. The ZIP file contains the files Triangle.java and TriangleTest.java
http://junit.sourceforge.net/doc/cookbook/cookbook.htm
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
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.)
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.
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.
When you are satisfied that your program works correctly ( or you run out of time ), please do the following.
· Your testcases
· The modified Triangle class
· originalTests.txt output
· newTests.txt output