Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
Getting Ready: Before going any further, you should:
downloads
directory/folder). In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above and then selecting eclipseskills
.
GiftCard.java
into the
default package (using the "Copy files" option).
GiftCard.java
.
GiftCardTest
in the default package by clicking on
File-New-JUnit Test Case.
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.
GiftCardTest
, replacing the test that was added by Eclipse.
@Test public void getIssuingStoreTest() { double balance; GiftCard card; int issuingStore; issuingStore = 1337; balance = 100.00; card = new GiftCard(issuingStore, balance); assertEquals(issuingStore, card.getIssuingStore(), "getIssuingStore()"); }
@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
?
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(int expected, int actual, String description)
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?
GiftCardTest
?
GiftCardTest
.
Why is it somewhat surprising that you can execute GiftCardTest
?
getIssuingStore()
method in the GiftCard
class so that it returns issuingStore + 1
, save
GiftCard.java
, and re-run the test suite.
Now what happens?
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.
getIssuingStore()
method.
Assert
class in JUnit also has a
static assertEquals()
method with the following
signature:
public static void assertEquals(double expected, double actual, double tolerance, String description)
where tolerance
determines how close two double
values have to be in order to be considered "approximately equal".
Add a test named getBalanceTest()
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
).
assertEquals()
in
one method (named, say, getIssuingStoreTest()
). How many tests
would be in your test suite?
Assert
class in JUnit also has a
static assertEquals()
method with the following
signature:
public static void assertEquals(String expected, String actual, String description)
Using JUnit terminology, add a test named deductTest_Balance()
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.
GiftCardTest
using EclEmma (Hint: You must click
on a different button.), click on the "Coverage" tab and expand
the directories/packages until you can see
GiftCard.java
and GiftCardTest.java
.
Why is the coverage of GiftCardTest.java
100% and why
is this uninteresting/unimportant?
GiftCard
class is correct?
GiftCard.java
?
GiftCard.java
.
What is different about it?
if
statement in the
constructor. What information appears?
deduct()
method in the GiftCard
class.
GiftCard
class. If this is the case, what is
different about the statements that remain untested?
try
-catch
statement
in the test. For example, add the following test
to your test suite.
@Test
public void constructorTest_IncorrectBalance()
{
try
{
new GiftCard(1, -100.00);
fail("Constructor, Negative Balance: Should have thrown an IllegalArgumentException");
}
catch (IllegalArgumentException iae)
{
// This is expected
}
}
GiftCardTest
using EclEmma, click on the
"Coverage" tab and expand the directories/packages until you can
see GiftCardTest.java
. Or, alternatively, click on
the tab for GiftCardTest.java
.
Why is the coverage of GiftCardTest.java
NOT 100% and why
is this unimportant?
constructorTest_IncorrectID_Low()
that covers the
case when the storeID
is less than 0.
GiftCard.java
now?
constructorTest_IncorrectID_High()
that covers the
other branch.
Copyright 2022