[No Audio]
In this video, we're going to explore
a really good way to structure those
unit test methods which you write, and that's
with the so called Arrange/ Act/ Assert paradigm.
So let's have a look at this now together.
So where assertions are useful in general
are at the end of a test. So this kind kind of brings
us onto the structure of tests.
So tests in general, are structured into
what's known as AAA formula, and that
stands for Arrange/ Act/ Assert.
So for example, if we just put the comments
here, arrange, act and assert, this is basically the
format that your tests should be taking. So a
unit test in general will be split into three sections,
within the test method. We've got an arranged
section, and that's basically where we're doing any
setup which we need to do. So that's
creating objects for the system and the test might
need, setting any data up that we need, setting up
any mocks or mock objects with any stubbed behavior
or canned answers that we want to return.
Join the test, those kinds of things.
That's the arrange section of the unit test.
Then in the act section, we generally
just have one line here, and that's
basically the method that's been tested. It's
that method being invoked, with whatever test
input we want it to run with for this test.
And then after that one method call to the
system under test, we have this assert section.
And this is basically going to be a few assertions
using that JUnit assertions API which we've just
seen, usually in conjunction with a matcher library
like Hamcrest, you're going to have those assertions
which effectively together check the state of the
world after the method's been executed, with the
test input that it was supplied.
So yeah, three sections.
Arrange/ Act/ Assert.
Always remember AAA, Arrange, Act,
Assert, and sometimes as well by the way,
Arrange/ Act/ Assert can be thought of as
Given-When-Then, which is popular in BDD
methodology. That's behavior driven development.
So you might see this being used as well.
But for now, as an implementer, a developer of the unit
tests, if you stick to Arrange/ Act/ Assert and always
have that in your mind when you're writing your tests.
And that should give you a good template
you can use to structure those tests when
you're cranking out those test cases.
Yeah, Arrange/ Act/ Assert always bear that
in mind and stick to it all the time.