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 skills
under the src
directory/folder.
GiftCard.java
into that directory.
GiftCard.java
.
GiftCardTest.java
in
the apropriate directory/folder by right-clicking on the
directory/folder, pulling down to New File..., and
entering the file name in the textfield.
GiftCardTest.java
,
after the package
statement but before the class declaration.
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test;What is the purpose of these statements?
GiftCardTest.java
, after the declaration of the class.
@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
?
skills
folder by clicking on the greater
than sign that is next to it in the testing view. What happens?
You can execute all of the tests in a file in two ways. (1) You can hover the cursor over its name in the testing view and clicking on the icon. (2) You can hover the cursor over the class declaration and click on the icon.
You can execute one test by hovering the cursor over the method declaration and clicking on the icon.
GiftCardTest.java
.
Why is it somewhat surprising that you can execute GiftCardTest.java
?
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 test results view are 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 JaCoCo, first click on
the checkbox in the status bar (at the
bottom of the VSCode window) and re-execute all of the tests
in GiftCardTest.java
. (Note: DO NOT click
on "Run Test With Coverage", click on "Run Test".)
Why is the coverage of GiftCardTest.java
uninteresting/unimportant?
GiftCard
class is correct?
GiftCard.java
.
What is different about it?
GiftCard.java
covered?
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.java
and why is this unimportant?
constructorTest_IncorrectID_Low()
that covers the
case when the storeID
is less than 0. What code did you add?
GiftCard.java
covered now?
constructorTest_IncorrectID_High()
that covers the
other branch.
Copyright 2024