Download Test Driven Development - Software Engineering I - Lab 4 | CSI 3471 and more Lab Reports Software Engineering in PDF only on Docsity!
CSI 3471
Lab 4: Test-Driven Development (TDD)
Principle : Even before you implement a class you should develop the test software
for that class. In particular, the test routine for a function should be written
prior to the function.
Procedure : You will develop a class called Tuple in conjunction with the class called
TupleTest, using Eclipse and JUnits, a testing framework within Java.
Your real goal in this case is to test compareTo. But you will do it by
testing three other functions, isEqual, isLess, and isGreater that call
compareTo.
1. Create a project, call it Name LabTDD, where Name is your family name. Within the
project create a source folder called src, and inside src create a package called mypkg.
Then define a class called Tuple with the following “skeleton”.
public class Tuple implements Comparable {
private int theX = 0; private int theY = 0;
public Tuple() { }
public Tuple( int x, int y) { theX = x; theY = y; }
public Tuple add( Tuple aTuple ) { return this; }
public boolean isEqual( Tuple aTuple ) { return false; }
public boolean isLess( Tuple aTuple ) { return false; }
public boolean isGreater( Tuple aTuple ) { return false; }
public boolean equals( Object anObject ) { return false; }
@Override public int compareTo(Tuple o) { // TODO Auto-generated method stub return 0; }
}
Note : The function equals overrides the function declared inside class Object and
compareTo is part of the Comparable interface.
5. Highlight JUnit 3, then click on the Add Library button. Then you will get the
following window. Highlight JUnit and hit Next.
6. Choose JUnit and hit Next. Then you will see the following:
9. Hit finish and you should see the following text, automatically generated by Eclipse.
package testingpkg;
import junit.framework.TestCase;
public class TupleTest extends TestCase {
public final void testAdd() { fail ("Not yet implemented"); // TODO }
public final void testIsEqual() { fail ("Not yet implemented"); // TODO }
public final void testIsLess() { fail ("Not yet implemented"); // TODO
public final void testIsGreater() { fail ("Not yet implemented"); // TODO }
}
For each of these functions you will write code to test a particular function inside
Tuple. To this end, declare the following Tuples within TupleTest as shown below.
Also define method setup, even though it will remain empty here. Method setUp is
executed at the start of a test run and tearDown is the opposite of setup, executed at
the end of the test function. Both setUp and teardown are declared within TestCase.
At this point you should now have something similar to the following:
public class TupleTest extends TestCase {
private Tuple tuple14 = new Tuple( 1, 4 ); private Tuple tuple14b = new Tuple( 1, 4 ); private Tuple tuple15 = new Tuple( 1, 5 ); private Tuple tuple53 = new Tuple( 5, 3 ); private Tuple tuple54 = new Tuple( 5, 4 ); private Tuple tuple00 = new Tuple();
protected void setUp() { } · · ·
and so on. We will assume that a Tuple can be formed using any non-negative integer
(that is less than or equal to maximum integer allowed).
10. Now define testEqual as follows. The basic concept: Execute the function to be tested
and compare its results to what is expected. The JUnit framework provides us with
several ways to judge if a test has succeeded or failed. They include
• assertEquals: to determine if an actual value matches the expected one
• assertFalse: the Boolean expression is false
• assertNotNull: the parameter is not the Null pointer
• assertNotSame: If your method is supposed to return an element from a list
you can use this to check if the element returned is the one from the actual list
• assertNull: the parameter is the Null pointer
• fail: will force a failure; used with a description string as a parameter
For example, assertTrue is a built-in function that will essentially “report” the
results of the test to the JUnit testing “execution engine”.
1
(^1) Think of this as the surrounding code provided by the JUnit framework that orchestrates the execution of
each JUnit test object.
13. At this point you are ready to execute the JUnit. Highlight TupleTest in the
PackageExplorer window of Eclipse and use the right mouse button to chose Run As
Æ JUnit Test.
The Eclipse window should now look like the following:
Notice that the left side of the window now displays the JUnit tab (instead of the Package
Explorer). There is a red bar visually indicating that the tests failed. Also, when a test
function name is highlighted in the upper window the results of the test are listed in the
bottom window. So, in this example testAdd is “Not yet implemented”, consistent with
what you would expect.
Highlight testIsEqual in the JUnit tab and you will see that there are no error messages in
the lower window.
12. At this point comment out the test functions that are not defined within TupleTest and
execute the TupleTest again, as you did above. The results should appear similar to the
following, with a green bar indicating that all of the tests “passed”.
- Creates Tuple
- @precondition true
- @postcondition true */ public Tuple()
- Creates Tuple
- @precondition (x >= 0) && (y >= 0)
- @postcondition (theX == x) && (theY == y)
- @param x
- @param y */ public Tuple( int x, int y)
- Is this Tuple equal to aTuple?
- @precondition true
- @postcondition (compareTo(aTuple) == 0)
- || (compareTo(aTuple) != 0)
- @param aTuple
- @return whether this equals aTuple */ public boolean isEqual(Tuple aTuple)
- Adds aTuple to this Tuple
- @precondition true
- @postcondtion (result.theX == theX + aTuple.theX) &&
- (result.theY == theY + aTuple.theY)
- @param aTuple
- @return sum of this and aTuple */ public Tuple add(Tuple aTuple)
18. Make sure that you have added the appropriate header at the top of each Java file and
that you have uploaded your project to your SVN repository.