Question
Grade.java /** * An encapsulation of a gift card that can be used to * make purchases at a retil store. */ public class GiftCard
Grade.java
/** * An encapsulation of a gift card that can be used to * make purchases at a retil store. */ public class GiftCard { private static final int MAX_ID = 9999;
private double balance; private int issuingStore;
/** * Explicit Value Constructor. * * @param storeID The ID of the issuing store * @param openingBalance The opening balance on the card * @throws IllegalArgumentException If either parameter is "illegal" */ public GiftCard(int storeID, double openingBalance) throws IllegalArgumentException { if (storeID < 0 || storeID > MAX_ID) { throw new IllegalArgumentException("Illegal Store ID: " + storeID); } else { issuingStore = storeID; }
if (openingBalance < 0.00) { throw new IllegalArgumentException("Illegal Balance: " + openingBalance); } else { balance = openingBalance; } }
/** * Deduct a (purchase) amount from this GiftCard. * * @param amount The amount to deduct * @return A message describing the result of the transaction */ public String deduct(double amount) { String result; if (amount < 0.0) { result = "Invalid Transaction"; } else { balance -= amount; if (balance < 0.0) { result = "Amount Due: " + String.format("%6.2f", Math.abs(balance)); balance = 0.0; } else { result = "Remaining Balance: " + String.format("%6.2f", Math.abs(balance)); } } return result; }
/** * Get the remaining balance on this GiftCard. * * @return The balance */ public double getBalance() { return balance; }
/** * Get the ID of the issuing store. * * @return The ID */ public int getIssuingStore() { return issuingStore; } }
Questions:
1.Create an empty JUnit test named GiftCardTest in the default package by clicking on Use the most recent version of JUnit; if necessary, add JUnit to the build path when asked.
Note: Normally, you should put tests in their own package(s). To keep things simple, we will break that rule.
2. Copy the following code into GiftCardTest.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class GiftCardTest
{
@Test
public void getIssuingStore()
{
double balance;
GiftCard card;
int issuingStore;
issuingStore = 1337;
balance = 100.00;
card = new GiftCard(issuingStore, balance);
assertEquals("getIssuingStore()",
issuingStore, card.getIssuingStore());
}
}
3.
A JUnit test suite is a class, much like any other class. Tests are methods that are preceded with the annotation @Test. (Note: An annotation provides information about a program but is not part of the program. Annotations have no effect on the operation of the program. Instead, they are used to provide information to tools that might use the program as input.)
How many tests are in the test suite GiftCardTest?
4.JUnit has an Assert class that has a static assertEquals() method with the following signature that is used to compare expected and actual results:
public static void assertEquals(String description, int expected, int actual)
where description is a human- readable String describing the test, expected is the expected result, and actual is the result of actually running the code being tested.
How would you call this method and pass it the String "getIssuingStore()", the int issuingStore, and the int returned by the card object's getIssuingStore() method?
5. How is this method actually being called in GiftCardTest?
6. Why isn't the class name needed?
7.Execute GiftCardTest. Why is it somewhat surprising that you can execute GiftCardTest without a main method?
8.What output was generated?
9.To see what happens when a test fails, modify the getIssuingStore() method in the GiftCard class so that it returns issuingStore + 1, compile GiftCard.java, and re-run the test suite.
Now what happens?
10.In the "Failure Trace", interpret the line:
java.lang.AssertionError: getIssuingStore() expected:<1337> but was:<1338>
Note: You may have to scroll the "Failure Trace" window to the right to see the whole message.
11.What mechanism is JUnit using to indicate an abnormal return?
12.Before you forget, correct the fault in the getIssuingStore() method.
13. The Assert class in JUnit also has a static assertEquals() method with the following signature:
public static void assertEquals(String description, double expected, double actual, double tolerance)
where tolerance determines how close to double values have to be in order to be considered "approximately equal".
Add a test named getBalance() that includes a call to assertEquals() that can be used to test the getBalance() method in the card class (with a tolerance of 0.001).
14.How many tests are in your test suite now?
15.Suppose you had put both calls to assertEquals() in one method (named, say, getIssuingStore()). How many tests would be in your test suite?
16.Re-compile and re-execute the test suite. How many tests were run?
17.The Assert class in JUnit also has a static assertEquals() method with the following signature:
public static void assertEquals(String description, String expected, String actual)
Using JUnit terminology, add a test named deduct() to your test suite that can be used to test the deduct() method in the GiftCard class. Note: Be careful, the variables that are declared in the getIssuingStore() method are local.
18.Execute the test suite.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started