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