Test-Driven Development Lab
CSC 4700 Software Engineering
This is a hands-on lab activity (worth 20 points) that will broaden your understanding of test-driven development, JUnit and the use of Eclipse. There is nothing to hand in, but you should show your results to your instructor to receive credit.
Run Eclipse IDE.
Create a new Java project, so click File -> New -> Java Project. Type
in a project name, for example, ProjectWithJUnit
. Click Finish.
The new project will be generated in your IDE.
Next, configure the Eclipse IDE to make sure that the JUnit library is in the build path. Click on Project -> Properties, select Java Build Path, Libraries, click Add Library and select JUnit. Click Next, and then select version 3 of JUnit it it is available, and click Finish. You will see that JUnit will appear on your screen in the list of libraries.
Next, create a class called "MathTools".
Add one or more methods to the "MathTools" class, such as:
public class MathTools { static public int add(int a, int b) { return a + b; } static public int multiply(int a, int b) { return a * b; } }
Next, create a test case, right-click on the ProjectWithJUnit
title, select New -> Other, expand the "Java" selection, and choose
JUnit. Expand the JUnit selection and choose Test Case, then click
Next. This is illustrated by Figure 1.
Figure 1. Creating a JUnit test in the Eclipse IDE
Type in the name of the test case, such as TestMathTools
into
the Name field, click on Finish.
Edit the code for TestMathTools.java is as follows:
import junit.framework.*;
public class TestMathTools extends TestCase {
public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 6;
int sum = 0;
sum = MathTools.add(num1, num2);
assertEquals(sum, total);
}
}
This code is not complex; it's just a bit unusual. However, let's examine it
in detail. We extend JUnit's TestCase
class. Ultimately, and method
in the TestCase class will be automatically run if it starts with "test".
Now, try running the test. Right-click on TestMathTools.java in the Package Explorer on the left, and select Run as -> JUnit Test. The test should fail if you used the test code above. Note that there is a helpful message in the "Failure Trace" box that can help you pinpoint the problem.
Figure 2. Failed test in JUnit
Examine both your "add" method and your "testAdd" test and try to identify and correct the error. Rerun the test.
Next, add a few more methods to your "MathTools" class, and add a test for each method to your "TestMathTools" class. Rerun all of your tests.
Next, create a "stress test". This is a test that will run a method through a large range of inputs and test the results for all of the inputs. For example, here is a stress test for the "add" method:
public void testAddStress() { for (int num1=0; num1<1000; num1++) { for (int num2=0; num2<1000; num2++) { int total = num1 + num2; int sum = MathTools.add(num1, num2); assertEquals(sum, total); } } }
Next, examine the API for JUnit and add a few additional methods and test cases that use other Assert class methods, such as assertArrayEquals, assertTrue, assertFalse, assertNull and fail.
Demonstrate your results to your instructor to receive credit.
03/26/2018