Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Unit Testing in Java!
You have completed Unit Testing in Java!
Preview
Let's learn how the IntelliJ integrates running tests and outputting the results.
Learn more
- Static imports
- InfiniTest - Continuously run tests automagically!
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
All right, so
getting it set up was super easy, right?
0:00
Wait till you see how
easy it is to run this.
0:03
So easy, in fact, that you should get in
the habit of running them all the time.
0:06
That's kind of the concept.
0:10
If you aren't working in
an environment where you're using test
0:13
driven development,
0:15
or TDD, you should make sure that you
test as often as you make changes.
0:16
Tests are meant to run super fast.
0:21
You'll be surprised how quick they are.
0:23
Some people have it set up so
0:26
that when you save the tests run
in the background automatically.
0:27
Let's go get you comfortable
running these tests.
0:30
>> Okay, ready?
0:34
So here's what you do: you right
click on the test directory, and
0:35
you choose Run All Tests.
0:40
And there we go, they ran.
0:43
1 test passed in 1 ms.
0:45
And when we did that,
it created a new run configuration so
0:47
you don't need to do that again.
0:51
You can just press the Play button here,
right?
0:52
You can also press the hot key Ctrl+R.
0:55
Okay, so
over here it shows you what tests run.
0:59
Right here is the test fixture,
CreditorTest and
1:02
here is the test method, testRefund.
1:05
And when they pass they turn green.
1:07
So wait a second, why did that pass?
1:10
It didn't do anything.
1:12
Well that's something
important to realize.
1:13
If no errors happen in the method,
it's assumed to pass.
1:15
Here, let me show you
how to force it to fail.
1:19
It's actually a method called fail.
1:21
So where is that coming from?
1:25
That's coming from this import line here,
this org.junit.Assert.* But,
1:27
if you notice something different
there's an import static here,
1:32
there's a different keyword here.
1:34
So, this is a pattern that's kinda handy.
1:36
If you have a class that has
a bunch of static methods
1:39
on it as this asset class does,
you can do import static.
1:43
And then instead of having
to type assert.fail,
1:46
which is the static method off of
the assert class, you can just type fail.
1:49
And it turns out that they are a bunch
of static methods on the Assert class.
1:54
And this is the common
practice to do in junit
1:58
is to expose this in your name space so
that you can use them quickly and easily.
2:01
And check the teacher's notes for
more information.
2:05
All right.
2:07
So now that we have a call to this
static method fail of the Assert class,
2:08
it should fail, let's make sure.
2:12
Boom.
That's a red.
2:16
That's a gross color to have, right?
2:17
One test failed.
2:18
And if you look over here it
shows you where it failed at.
2:20
The exclamation point.
2:23
Now if there where more tests, we'll see,
well let's just make a new one right.
2:24
So if we can use code
generation to help us.
2:27
So if we do command N and
choose generate test method.
2:29
It's going to ask us to change the name.
2:33
We can just call it example, for now.
2:35
Let's do that.
2:38
And let's run those tests again.
2:38
And you'll see now that
the fixtures mark as failing.
2:40
But one of them passed,
then one of them failed.
2:45
If you see down here to there is
a one passed one failed, as well.
2:48
I'll tell you how many paths I have,
how many times it ran here and
2:53
how many times it failed.
2:56
No no, when this method was created it
was generated with the test prefix.
2:58
Again this is totally one of those
old styles that's no longer needed.
3:03
What you want it to do is define
what it is that you're testing and
3:06
what it is that you're expecting.
3:10
So what do you say we make the test
verify that when you add money,
3:12
available funds are updated?
3:16
Shall we?
3:19
So, let's rename this test to
3:20
addingFundsIncrementsAvailableFunds.
3:24
It's pretty clear, right?
3:30
Now, if we saw this failing,
3:31
we'd know right from the title
what was going on, right?
3:32
We know uh-oh, when we add funds it's
not incrementing the amount available.
3:35
Pretty clear, right,
if we name it properly.
3:39
So we need to write the body of this
method to make sure that it happens.
3:42
So first we'll set up the environment,
then we'll add the money, and
3:45
then finally we'll check or
assert that available funds were changed.
3:48
Let's learn how to write that test
write after this quick break.
3:53
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