I have a class Inventory which has 6 methods. I am supposed to implement tests for each on of these 6 methods in a test class called InventoryTEST. The class object is a hashmap.
Here are the methods for the class
/**
* Return the number of Records.
*/
public int size() { }
/**
* Return a copy of the record for a given Video.
*/
public Record get(VideoObj v) { }
/**
* Return a copy of the records as a collection.
* Neither the underlying collection, nor the actual records are returned.
*/
public Collection toCollection() {}
/**
* Add or remove copies of a video from the inventory.
* If a video record is not already present (and change is
* positive), a record is created.
* If a record is already present, <code>numOwned</code> is
* modified using <code>change</code>.
* If <code>change</code> brings the number of copies to be less
* than one, the record is removed from the inventory.
* @param video the video to be added.
* @param change the number of copies to add (or remove if negative).
* @throws IllegalArgumentException if video null or change is zero
* @postcondition changes the record for the video
*/
public void addNumOwned(VideoObj video, int change) {
}
/**
* Check out a video.
* @param video the video to be checked out.
* @throws IllegalArgumentException if video has no record or numOut
* equals numOwned.
* @postcondition changes the record for the video
*/
public void checkOut(VideoObj video) {
}
/**
* Check in a video.
* @param video the video to be checked in.
* @throws IllegalArgumentException if video has no record or numOut
* non-positive.
* @postcondition changes the record for the video
*/
public void checkIn(VideoObj video) {
}
/**
* Remove all records from the inventory.
* @postcondition <code>size() == 0</code>
*/
public void clear() {
}
Now in order to test these methods I will need to create an object with some records in it. One way I could do this is call the default constructor which will give me an empty hashmap and then call addNumOwned to add records to the hashmap. Another way I could do this is create an overloaded constructor which can add records in the hashmap at the time of creation.
The issue I see with the first method is addNumOwned is a method (and unit) being tested in the test class. So if this unit fails all the other ones will as well. Should a unit test be suspectible to the failure of another unit test?
I suppose if I had to make a unit test for the constructor the same argument would hold. I do not have a unit test for the constructor, however.