Thanks Greg. You actually answered my question perfectly. Here is the code I was running.
This is a JUnit test class I created in eclipse -
package edu.ncsu.csc216.cash_register;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
/**
* @author Richard Meyer
*
*/
public class CurrencyCollectionTest {
/** New CurrencyCollection object for testing of array
* currency objects. */
private CurrencyCollection cashDrawer;
/** New array of Currency objects for testing of
* individual currency objects. */
private Currency [] currency;
/**
* Sets up CurrencyCollectionTest by creating a CurrencyCollection
* object to act as a cash drawer.
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
//Initialize cashDrawer for CurrencyCollection array testing.
cashDrawer = new CurrencyCollection(10);
}
/**
* Create a currency object to test the object properties match the index
* of the CurrencyCollection object in the constructor.
* Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
* #getCurrencyAtIdx(int)}.
*/
@Test
public void testGetCurrencyAtIdx() {
//Currency object with the value of a nickel and a count of 10.
Currency nickel = new Currency(CurrencyCollection.NICKEL_VALUE,
CurrencyCollection.NICKEL_NAME, 10);
//Test nickel currency object exists in expected index location.
assertTrue(nickel.equals(cashDrawer.getCurrencyAtIdx(1)));
//Ensure the method can cope with index out of bounds exceptions.
try {
//Call currency index out of bounds
cashDrawer.getCurrencyAtIdx(8);
fail(); //We should never reach this point, if we do, the test fails
} catch (IndexOutOfBoundsException e) {
}
}
/**
* Test by increasing the amount of a denomination by a set value and
* ensuring the count has been increased using the getCount() method.
* Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
* #modifyDenomination(int, int)}.
*/
@Test
public void testModifyDenomination() {
//Modify denomination by 4 five dollar bills.
cashDrawer.modifyDenomination(500, 4);
//Store currency collection in currency array to call up
//individual denominations.
currency = cashDrawer.getCurrencyCollection();
//We should expect to see 14 five dollar bills total,
//10 initial + the 4 we just added.
assertEquals(14, currency[5].getCount());
//Ensure the method can catch an invalid currency value.
try {
//Pass a denomination that does not exist
cashDrawer.modifyDenomination(10001, 2);
fail(); //We should never reach this point, if we do, the test fails
} catch (IllegalArgumentException e) {
}
}
/**
* Create a new CurrencyCollection payment object to simulate currency
* deposit. Verifies the deposit by calling the getBalance() method and
* compares it with the expected amount.
* Test method for {@link
* edu.ncsu.csc216.cash_register.CurrencyCollection
* #depositCurrencyCollection(edu.ncsu.csc216.cash_register.
* CurrencyCollection)}.
*/
@Test
public void testDepositCurrencyCollection() {
//create a CurrencyCollection object to simulate purchasing an item.
CurrencyCollection payment = new CurrencyCollection();
//Collect 5 dollars and put into new payment object.
payment.modifyDenomination(500, 1);
//Deposit the payment into the cashDrawer.
cashDrawer.depositCurrencyCollection(payment);
//Verify the total in the cashDrawer reflects the payment.
//Total should equal starting amount + 500.
assertEquals(36910, cashDrawer.getBalance());
}
/**
* Simulates a Currency refund by passing a refund amount to the cashDrawer
* object, and ensuring the getBalance() method returns the expected
* refund amount.
* Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
* #refundByAmount(int)}.
*/
@Test
public void testRefundByAmount() {
//Pass a refund amount (amount for a t-shirt).
cashDrawer.refundByAmount(1827);
//Ensure cash drawer total is reduced by refunded amount.
assertEquals(34583, cashDrawer.getBalance());
}
/**
* Creates a new CurrencyCollection array to compare with CurrencyCollection
* in our constructor method.
* Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
* #getCurrencyCollection()}.
*/
@Test
public void testGetCurrencyCollection() {
//Create new CurrencyCollection object for this method only.
CurrencyCollection collectionTest = new CurrencyCollection(10);
//Compare the new CurrencyCollection object with the CurrencyCollection
//object defined in the constructor method as arrays.
assertArrayEquals(collectionTest.getCurrencyCollection(),
cashDrawer.getCurrencyCollection());
}
/**
* Check the total amount in CurrencyCollection object.
* Test method for {@link edu.ncsu.csc216.cash_register.CurrencyCollection
* #getBalance()}.
*/
@Test
public void testGetBalance() {
//check initial amount in cashDrawer.
assertEquals(36410, cashDrawer.getBalance());
}
}
So the code in the @Before tag (setUp() instruction) must have ensured that I have a fresh cashDrawer object for use with each method, right? That means if I manipulate the object in one of my test methods, it will be "re-initialized" by my setUp() method for use on the next method, right?