This course will be retired on July 14, 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
- The Parts of a Room Database 3:45
- Creating the PizzaTopping Entity 5:50
- Choosing Queries 1:47
- Creating the First Dao 3:45
- Finishing the Daos 4:17
- Room Beginnings 6 questions
- Creating the RoomDatabase 4:10
- Testing the Database 6:05
- Saving a Test Pizza 4:54
- Saving a Pizza's Toppings 5:16
- Room Endings 5 questions
Preview
Start a free Courses trial
to watch this video
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Testing is an important part of the development process. In this video we'll start implementing testing in the app!
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
We just saw how we can access our
database from within the app.
0:00
But before we start using our database,
0:03
let's write a few tests to make sure
we won't hit any unexpected turbulence.
0:05
If you're new to testing, don't worry.
0:11
I'll try to explain things as we go along.
0:13
But if you'd like to have a complete
understanding of how testing works on
0:15
Android, check out the course linked
in the teacher's notes below.
0:18
All right, let's start by undoing
what we did in main activity,
0:22
and then let's open the project pane.
0:28
Here, in addition to our project files,
0:31
we have two packages for
keeping all of our testing files.
0:34
The test package is for tests that
can run locally on your computer,
0:39
without needing to boot up Android.
0:43
On the other hand,
the Android test package is for
0:45
tests that require either an emulator or
an actual Android device.
0:49
Google recommends that we do room
testing on an actual device or emulator.
0:53
So we'll be using
the Android test package.
0:58
Inside this package Android's
given us an example test class.
1:00
So, instead of creating our own,
let's just use this one.
1:05
Let's put the cursor on the class name and
use Shift F6 to rename it to PizzaTests.
1:08
Then let's open it and
inside I'm going to delete this
1:19
comment to give us a little more room.
1:24
I'll also close the project pane.
1:27
Now that we've got our test class,
let's think about what we want to test.
1:30
For the most part, we should be testing
that we can save and retrieve a pizza.
1:34
We'll write one test to verify saving and
retrieving from the pizza table and
1:38
another to verify the PizzaToppings table.
1:43
We could also write tests around deleting
data, as well as the topping class.
1:46
But since testing isn't the focus here,
we'll just leave it at these two.
1:50
Okay, so first things first, we'll need
to create a pizza to use in our tests.
1:55
Let's ignore this test function for now,
and add some room at the top of the class.
2:00
Then let's declare a new
val named testPizza and
2:05
set it equal to 0, a new Pizza with
an ID of 0, a name of Hawaiian.
2:11
And for the creation date,
let's just pass in a new date object,
2:23
which will give us a creation
date of the current time.
2:29
And I'll go ahead and
minimize my imports again.
2:34
Now that we've got our pizza,
2:37
let's create the list of topping
IDs we'd like to use with it.
2:39
On the next line, let's create a new val,
named testToppingIds.
2:43
And then let's flip over to our app
class to see which ids we need.
2:49
Since we don't have ham as an option
let's go with pepperoni and
2:56
pineapple, which have Ids of 1 and 7.
3:02
So back in our test class let's set
3:06
testToppingIds equal to list of 1 and 7.
3:11
Awesome with both of our test pieces
ready to go we're ready to start testing.
3:17
Let's start by renaming
this test function.
3:22
U shift F6 And let's go with pizzaTest.
3:25
Then inside the function let's get rid of
the comment, As well as the assertion.
3:31
And now that we've just got a context,
let's create a new val named db and
3:40
set it equal to an instance
of our database.
3:46
If you'd like to try and remember how to
do it on your own, pause me for a second,
3:49
and give it a shot.
3:53
Okay, here we go,
Room.databaseBuilder| appContext,
3:54
then the class which is
_PizzaDatabase class.java,
4:03
then the name of the database,
PizzaDatabase.
4:10
And then a call to build,
4:22
which if it's getting all the way over to
the right you can put on the next line.
4:24
Great, let's keep things simple by
ignoring the topping IDs for now and
4:29
just try to insert our test pizza into
the pizza table and then get it back out.
4:33
On the next line let's type db.pizzaDao()
to give us access to our pizzaDao.
4:38
Then let's call the insert function and
pass in our testPizza,
4:45
to insert the pizza into the database.
4:50
Next, we need to retrieve the pizza, and
verify that it matches our testPizza.
4:52
On the next line, let's create
a new val named returnedPizza,
4:58
And set it equal to
db.pizzaDao() .getPizzsById and
5:04
pass in the ID of our test pizza,
testPizza.id.
5:11
Finally, we just need to assert that
our testPizza is the same as our
5:16
returned pizza.
5:20
On a new line,
with that a call to assertEquals and
5:22
then pass in our testPizza for
the expected value and
5:27
the returnedPizza for the actual value.
5:33
All right, Let's click the play button
next to our test and see what happens.
5:38
Remember, this test requires
an Android device, so
5:45
if you don't have one connected
you need to start up the emulator.
5:47
Yikes, the app didn't even compile.
5:57
Let's take a short break, and when we get
back, we'll take a look at this error.
6:00
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