Characterization Testing: Hand¶
We'll continue characterizing and refactoring the display methods in Hand
in this lab.
We're Done When...
We know we're done when there are we have removed all System.out.print
statements from the Hand
class, as well as any "display" methods that return a String.
A. Refactor Hand's Display Face Up Card¶
First, let's tackle the displayFaceUpCard
method.
-
As you did in the previous lab, create a characterization test for
Hand.displayFaceUpCard()
, like this:Start With...
package com.jitterted.ebp.blackjack; import org.junit.jupiter.api.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; public class HandDisplayTest { @Test public void displayFaceUpCard() throws Exception { Hand hand = new Hand(List.of(new Card(Suit.HEARTS, Rank.ACE))); assertThat(hand.displayFaceUpCard()) .isEqualTo(""); } }
-
For the refactor, the transformer (
ConsoleCard
) will need access to the Face Up card so:- Create a public
dealerFaceUpCard()
query method onHand
that returns the firstCard
from the list. You can do this via an Extract Method refactoring.
- Create a public
-
Refactor
displayFaceUpCard
to be a static method, and then move it (via Move Static Method) to a new class namedConsoleHand
. Be sure to run the tests!
B. Repeat for Hand's Display¶
Don't forget to run all tests after each step and make sure that they pass before moving on.
-
Write a characterization test for Hand's
display()
method. Since it doesn't return anything, you will have to refactor it to split it into two methods:-
One method that returns a String (named
cardsAsString
) and -
Another that does the
println
of the String.
Note
Be sure to add two cards to the Hand for this test to ensure you capture the behavior of a two-card Hand.
-
-
Create a public
cards()
query method on Hand that returns aList<Card>
.Tip
Since we want to continue to encapsulate Hand's list, be sure to return a copy of the internal collection.
-
Refactor your new
cardsAsString
method to be static and then use Move Static Method (shortcut key is F6) to move it toConsoleHand
. -
Get rid of the
System.out.println
reference in thedisplay()
method by inlining it intoGame
.
C. Complete "Purification" for Hand¶
Since displayValue()
also returns a String that's meant for console output, we need to remove it to complete the purification of the Hand class. You will have to make the tradeoff between hiding the value()
method, and purifying the class. Remember that pure domain outweighs the desire for encapsulation.
Once you remove the displayValue()
method, and expose the value()
, what tests need to be changed? Hint: is valueEquals()
still needed?
Note
Make sure all the tests pass.
You are done!