



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lab guide for cs 331 students to learn how to use junit, a testing framework, in java. The lab focuses on running tests, writing tests, and fixing bugs related to array lists. Students will learn how to compile and run tests using junit, interpret error messages, and write simple test cases.
Typology: Lab Reports
1 / 5
This page cannot be seen from the preview
Don't miss anything!




The title of this lab is “Array List”, but is really about JUnit, the testing framework we will be using in this course.
Your objective is to become familiar with the Java environment we will be using in the course, and in particular the JUnit tool.
In this lab, you will see how to run tests, fix a bug, write your own test, and fix a second bug. To fetch the files you need for this part of the lab, go to your working copy of the repository and type (remember that % is the prompt).
% git pull
This will create a directory called array-list-lab. Go ahead and change to that directory.
2.1 Running the Tests 2 JUNIT
Inside the directory you will find three files.
Using your editor, bring up the TestArrayList.java file and look at the contents. All JUnit tests have a similar skeleton:
import junit.framework.*;
public class TestArrayList extends TestCase {
public TestArrayList(String name) { super(name); } }
The import line tells it to find the JUnit library code. Next, you have to define the class, in this case TestArrayList, and have it inherit from the TestCase class supplied by JUnit. The file you have will have a bunch of Integer constants defined, and then a constructor, which usually doesn’t do very much. Finally, you’ll notice that there are several methods that begin with the string “test”. Note that the test is lower case. These signal JUnit that these are testing methods. Notice how each method tests a different aspect of the ArrayList class. To run the tests, first compile the files. To make this easier, we’ve included a Makefile that defines several commands for the make command. (These were explained briefly in the last lab.) The targets included are:
2.2 Writing tests 2 JUNIT
Tests run: 2, Failures: 1, Errors: 0
make: *** [tests] Error 1
The error says there’s a bug in testAdd. Finally, you get to do some programming! Well, debugging anyway. Normally, this tells you the line number on which the assertion failed. You will also see that test was expecting 1, but got 0, and the test case that caused it was testAdd. Look at the testAdd and see that the problem is in the line assertEquals(foo.size(),1);. (At least, that’s the only thing expecting a 1.) Now look at ArrayList.java, find the bug, and fix it. Rerun the tests to make sure the bug is really gone. Once it is, commit your files to the repository using git commit -am ’Fixed array bug’. You should also push the changes to the server with git push, but you can wait until the end of your work session to do this if you like. You can use a different comment string if you like. Always commit your files after each “unit” of work has been done. Another way to think of it is to ask, “have I done enough work that I’d be upset if it all got erased?”. If so, run git commit. Remember to use git add on files that you created or modified so that they are tracked too, but please don’t add .class files (or other computer-generated files) to the repository.
The add method has not been tested fully. We do not have a test for what happens if we overfill the array. Write a new test called testOverfill. Have it create a small ArrayList, maybe a capacity of 5 elements, and then try to add 6 elements to it, and then try to find those 6 elements. What happens? Once you have a test that correctly finds the error, commit the change to your repository. Finally, fix the bug, verify that the test runs correctly.
To turn in your lab, push all your files to the remote repository. Use git clone to verify that the files you wanted in are there. Again, please don’t add .class files and the like to the repository.
2.3 Handing it in 2 JUNIT
There is a grading script we use to score your labs. That script will start on Wednesday, and will run for one week. While the script is running, you can have as many regrades as you want, simply by pushing your code into the server’s repository. More information about the script will be sent out when the grading starts.