This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll create a test for our tableau piles to see if we're adding cards correctly!
Testing Comments
// arrange
// act
// assert
Related Links
Project Files
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
If you're not so
0:00
sure you implemented something correctly,
it's always a good idea to add some tests.
0:00
Let's add tests to our tabloid pile
class to let us know if our addCards and
0:05
removeCards functions are working
like we want them to.
0:09
And before I get to that, I want to make
sure that this is the addCards function.
0:13
Okay, now let's start by first creating
a test folder for our project.
0:19
First, create just a regular directory
named Test in the root of your project.
0:23
Then open the project structure window,
file, project structure,
0:31
and on the Modules tab, select your
test folder and mark it as Tests.
0:38
Then hit OK and
now we've got a spot for our tests.
0:46
Next up, let's create the tests.
0:50
Click somewhere in your Tableau
pile class and then use Command or
0:53
CTRL+SHIFT+T and
hit Enter to create a new test.
0:57
Then for the testing library,
make sure you've selected JUnit4 and for
1:02
the members, let's pick addCards and
removeCards.
1:07
Also if you have something up here
that says fix, just click on it and
1:12
pick the intellijay distribution.
1:17
Then hit OK, and now it's testing time.
1:19
Remember for each test,
we need to do three things.
1:22
We need to arrange the objects we need,
act on those objects, and
1:26
then assert that we got the right result.
1:31
So let's start by adding
in these steps as comments.
1:33
You can find them in the teacher's
notes below as well.
1:36
All right, let's take it from
the top with the addCcards function.
1:45
To test that our addCards function is
working, we're going to need one Tableau
1:49
pile and one mutable list of cards
to add to that Tableau pile.
1:53
So in the Arrange section, let's
start by creating a new Tableau pile.
1:58
val tableauPile
2:01
= TableauPile.
2:06
And then let's initialize our Tableau
pile to contain only the King of spades,
2:11
mutable list of card with a value
of 12 for King and then spades.
2:16
On the next line,
let's create a new variable for
2:23
the cards we're going to
add to our Tableau pile.
2:26
Val cards and
let's set this equal to a list of
2:30
one of the cards that would come after
this one, or choose the Queen of hearts.
2:34
So mutable list of and
2:39
then the Queen of hearts, which has
a value of 11 and a suit of hearts.
2:42
Moving on to the act section,
2:47
let's try adding that cards
list to our Tableau pile.
2:49
tableuPile.addCards and
pass in our cards list.
2:52
And finally in the assert section,
let's check that our Tableau has the right
2:58
number of cards which sends red Queen
does match with the black King is two.
3:02
So assertEquals 2 for
the expected value and for
3:09
the actual value, tableauPile.cards.size.
3:14
Then let's hit the Run
button next to our test.
3:19
And it looks like the addCards function
works, well, at least for those two cards.
3:27
Anyhow in the next video,
we'll continue the quest for
3:33
testing with our removeCards function.
3:36
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up