📺 Develpreneur YouTube Episode

Video + transcript

AssertJ Part 2

2023-01-19 •Youtube

Detailed Notes

Welcome to another edition of Developreneur. Today we are going to be talking about AssertJ packages. AssertJ is a fluent assertion library in Java for unit testing. Therefore, instead of using the traditional JUnit Assert class, we will discuss this new library, AssertJ. Today we'll be looking at unit testing with Assert J. We'll briefly talk about the differences between assertions and why they're essential within unit testing. And then, we're going to go into a full HandsOn presentation using assert j methods and chaining within our unit testing.

What is Unit Testing?

Let's begin by talking about unit testing. Unit testing is software testing, where we test the individual units of our source code. Here we focus on testing particular methods or units of work, not the general application. We're not doing regression or integration testing on the different integration points or other applications. We want to focus on a specific unit of work, potentially a method in our code.

Why is unit testing necessary?

First, it helps us to identify problems early in the development cycle. Then it lets us quickly catch bugs or issues with our code changes at development time. So as we're writing the code, we can test the code and say, oops, our change broke something, or everything passed, and we can then move on. Finding and fixing bugs at this level is very cost-effective because we can fix them now. Whereas if we waited till we did regression testing or manual testing of the application once it's been packaged and shipped. It will be harder for us to identify what that bug is or where that problem came from in the code.

Finding and fixing bugs at this level is very cost-effective because we can fix them now.

The importance of Unit testing

With Unit testing, we can quickly catch most of the functionality bugs within our application during development. It also makes debugging the process easier because if we see the bug in our unit test, we know specifically in our code where it broke. We know where to fix the problem. Again, if we wait till the end when we're trying to package and ship the product, we don't necessarily know where the bug exists. So it will take more time and effort to go back in and dig into the code and find the problem. The other nice thing about unit testing is we can reuse our code to migrate our specific test from one application to another. So if we have similar code in one application, we could copy that unit test, put it into the other code base, make a couple of tweaks, and be done.

Why is unit testing so effective?

Unit testing is the first line of testing that developers can do while writing their source code. Now, this leads to assertions. So what are assertions? The Oxford Dictionary defines assertion as a confident and forceful statement of fact or belief. Which translates into what we're looking at when we talk about testing. Junit or unit testing defines an assertion as a collection of utility methods that support asserting test conditions. It is easy to take a test scenario or some test data and test something. We want to make sure that things work.

An assertion is a confident and forceful statement of fact or belief.

So what is AssertJ?

AssertJ is a library or a third-party library that you can include in your project that is rich and easy to use. It is extensible and open source so that you can extend the libraries. It is also community-driven, so there are no licenses or costs to using this library. AssertJ provides a rich set of assert classes and methods that provide excellent error messages. It improves test code readability and is easy to use within your favorite IDE.

Why should I use AssertJ instead of plain Assert classes?

The whole reason I'm even talking about AssertJ today is that once I found and started using this, it made writing those unit tests, asserts, and use cases easier to read and follow. Instead of having to create a whole bunch of independent asserts with the traditional assert libraries that come with Junit or some of the other unit libraries. It made a difference in the way I write Unit tests.

Hands on with AssertJ

In the video, we also provide a hands-on demo of the different APIs between JUnit and AssertJ. Including a sample unit test highlighting some of the functionalities that come with AssertJ.

Finally, this series comes from our mentoring/mastermind classes.  These classes are virtual meetings that focus on how to improve our technical skills and build our businesses.  After all, the goals of each member vary.  However, this diversity makes for great discussions and a ton of educational value every time we meet.  We hope you enjoy viewing this series as much as we enjoy creating it.  As always, this may not be all new to you, but we hope it helps you be a better developer.

Transcript Text
thank you
[Music]
hello this is Rob with developmentor
also known as building better developers
wanted to announce that we have
school.developmentor.com feel free to
check it out if you like any of this
information any of the content that
we've sent and you would like to see
more you can come out you can enroll for
free we have free courses we've got
places for you to get better at just
learning a technology or how to's you
can work on your business skills we can
help you with becoming a better
developer as encoding and things like
that a lot of the stuff you've seen on
YouTube we also have out at
school.developmentor we just have it in
a little more of a educational format
and a way for you to track your progress
as you move forward becoming a better
developer
okay next we have collections so now not
only can we look at strings look at
objects we can actually take collections
so I've created a list of person
uh test people and I'm going to add John
Smith and Jane Doe to this collection
now with in this collection we can now
check to see so as you can see I copied
the uh
example from there from the assert.j
page so in their example they have all
these uh methods and classes that you
have to kind of include to see what's
going on it makes it hard to read
but here I just went with the simple
approach so we'll get rid of that line
so collection
is specific assertions and there are a
lot more so we can assert on a
collection not just a string but on a
collection we can check things like its
size we can check to see if it contains
the objects that we want we can also
check to see if it doesn't contain the
objects that we want so this is again
pretty slick pretty useful and very
readable we can assert that our test
people
our collection is two it contains John
Smith and Jane Doe and it does not
contain random people so if I run this
will pass
however if I switch this up and I put
random person here
okay so to take Jane Doe
take random person
now if I run this this should fail
because test people does not contain
random person but it does contain Jane
Doe but let's look at the nice error
message that we get to basically tell us
what we did wrong
so if we come over here
so Java assert error expecting person
with John Smith and Jane Doe to contain
uh John Smith and random person how and
um
so we failed at the second criteria
because it could not find random person
random person was not in our collection
which makes it very easy to look at this
and say ah okay so we asserted to test
people as to pass but we failed on this
line we failed because John Smith uh was
there but random person was not now what
if I
add random or change L back
now we should fail on this line
so if we run this again
read our error so again we're expecting
it writes out the whole collection for
us this is so slick very helpful so
we're basically saying okay we've made
it past the count we made it past the
expecting so we're expecting uh John
Smith
we're expecting Jane Doe but we're
expecting not to contain Jane Doe
however it did fine Jane Doe so when we
go back and look at our go back and look
at our test we know that we failed on
this line
so if I put uh
revert that back
now say we
want to check on this one so it has size
very easy to read expecting size zero
but was two and it tells you what was in
the collection
so again pretty straightforward fairly
easy to understand and the nice thing is
it's so readable it's so easy to follow
whereas if you just use standard
assertions again you're going to have to
create multiple lines of assertions do
multiple lines of checks and it's going
to get bloated and very ugly and very
unreadable very quickly
right so that's a very simple example of
using collections
let's move down to the as
method so as is used to describe the
test and will be shown before the error
message
so this is neat because now we can even
override
at test time what it is that we're doing
with our test so we can actually name uh
or describe what our test is doing
within the test again keeping it with
that very readable flow that assert J
gives us with the fluid so we have
assert that johnsmith.getage
dot s so we're going all right so we're
asserting that John Smith's age as we're
going to check the age
of John Smith
is equal to 65. now so we're going to
check percent s so we're using the
string
placeholder or strings and then we're
going to replace that with the actual
name from our objects we're going to
check John Smith's age is equal to 65.
so now just by reading this it's very
easy to read so we're going to assert
that John Smith's age as checking John
Smith's age is equal to 65.
now this may seem a little redundant
here but when we run this
we're going to see that on our report
it's very easy to read if it fail so
right now it passed so we're not going
to actually see anything but if our age
was wrong and I ran this
now we fail
so now
our comparison failure check John
Smith's age expected 64 but was 65. and
what's really cool is it actually tells
you or identifies what character was
different
so not only does it tell me that 64 is
not 65 but it actually points out that
it's the second the first number I'm
sorry the single unit number that was
different
again making this very easy to debug and
very easy to follow
all right so we'll put that back
so that is the as so that again is to
describe the test that you're doing
right so next say we want to test some
assertions
this one is straight from their example
I thought it was pretty clean very easy
to follow so I didn't want to go too far
off the rails so there's two ways we
could do this
so we can assert that
uh we can assert that we're going to get
an exception so we're going to assert
that a throne by so instead asserting
that
some check some pass is going to work
we're going to assert a throne we're
going to assert a uh an exception is
going to happen
then we're going to use Lambda and we're
going to essentially throw the assertion
and the message that we want
now
this is just a straight example we're
forcing the assertion we could
call John Smith's age or we could call
our calculator and we could trigger
whatever the action is that creates the
exception and then we can verify that
the message was thrown by using has
message
so to read this it is assert that thrown
by will happen when some execution
occurs and then we want to make sure
that we receive the message from the
exception
now if we wanted to use a little bit
more Behavior driven uh
testing style we can do it this way we
can do throwable Throne equals catch
throwable
again Lambda again using the throne new
exception and then we can assert that
Throne so we can assert that the
exception has message containing boom
so you could do either I actually like
this a little bit more but it does
require you to actually do the uh or
separate the layer between the exception
and the assert that so you could do it
this way which is everything's wrapped
together which is very clean or you can
do it this way where you actually
break the logic that creates the
exception and then you test the
exception
both ways work uh in both ways uh
generate the same message
so if I wanted to run this again this
will pass
and if we want to break it
and run
and expand this
so we see that the expected message was
supposed to be Boom at but we received
boom exclamation point
again exception assertion test arraylist
exception and assertion error now if I
put this one back
now it should fail on the second one
and as you see it is the exact same
message so again either approach Works
uh you get the same result
next let's look at extracting assertions
so here is another useful way we can use
Collections and assertions
so here I'm again going to create a
collection of persons I'm going to go
ahead and add all three people random
John Smith and Jane Doe
what's cool about this is using the
assert that method again we can pass in
a collection now not just an object and
then we can use this extracting method
to actually extract an individual class
from within our collection so I can take
person get name
and I'm not even just checking the class
I can actually check all the objects in
this collection based on their
um get name so I can base it on their on
a method so person get name
then I can use other comparative methods
like contains does not contain string
equal
and I can pass in a list of values to
check so I can say it does not contain
random does not contain person
another way of doing this we can
actually extract so this is for checking
a single value
with multiple checks on
uh get name
but we could also check multiple
so I could actually extract again
but this time not only am I extracting
get name but I can actually get name
page and State
so I don't even have to just use get
name I can actually use the variables
and then I can check to see if it
contains and this is cool where I can
use the Tuple method
which allows it's a utility method to
build a tuple basically you can
kind of chain out what it is that your
the values to match what it is you're
extracting so I can essentially create a
mock object with random person for name
page and State
all right so let me save this
run this
and this will pass
all right so let's take the first part
here
so random person so we're going to
assert that to uh the first one here
extracting person does not contain
random or person
so this time I'm actually going to look
for the whole name random person
so if I run this this should fail
because random person is in the list and
it does
it fails expecting random person John's
uh so it's expecting so the collection
I'm passing in is random person John
Smith Jane Doe not to contain random
person however it failed because random
person was in the list
again I am not writing any of these uh
uh I'm not writing any of these test
cases I'm not running any of this logic
this is all built into a search day
foreign
person and say we want to test the
second part here
and let's have Jane Doe fail we'll set
her age to be a hundred
run that again
we failed so we were expecting a
collection uh random person age state
uh John Smith Gage State Jane Doe H
state to contain random person 12. okay
that one looks good John Smith 65
Florida that looks good and whoops uh
we're looking for Jane Doe 100 that's
not there and it couldn't find them
so again very easy
to follow very easy
failures
okay and the last one we're going to
look at today is filtering a collection
so not only can you pull out values
within the collection you can actually
filter on the collection
so here and
spaces
all right so now we're going to take
that same collection that we used
previously
but we're going to filter on the
collection so we're going to assert that
our collection test people we are going
to filter on a character
in the name so a really character in
this case is person so we want person
so I'll change that to person
I like being descriptive so we have our
person object
person
so filter on
person dot get name dot contains J
so we're going to look for
all names so it's going to return me all
the names
or all the objects in the collection
that contain the name
with the letter j capital j so
this is going to return us should return
us just John Smith and Jane Doe
which we then can do that contains only
John Smith and Jane Doe
and this should pass so if I run this
we pass
however if I look for a random person
random person does not have J in the
name and this guy should fit
and it does so we're expecting person
John Smith Jane Doe to contain only
person John Smith Jane Doe random person
but could not find the following
elements person random person so we're
looking for that J
all right so we go back
and again so it contains only and then
we can do contains
we can do all
we can do exactly
we can say null
and we can even check sequence so
there's a lot of different things we can
do with this which is really cool
right so here again we're doing person
name dot contains J
contains only John Smith
all right and the last one we're going
to look at is combining filtering and
extracting so now we can actually it
just shows you that you can do more you
can do it pretty much anything once you
start this the stream once you start the
fluent path of assert that so here we're
going to start that test people
we're going to filter like we did up
here
again on person
and we're going to do person.name
contains capital j
okay filter containing only
again John Smith Jane Doe does not
matter on the order
so up to this point we are doing exactly
what we just did in the first part so
we're going to assert that the test
people with capital J in the name should
contain Jane Doe and John Smith
however we can go further we can then
once we get these two objects
we can extract person
then we can extract their age and then
we can double check to make sure that
the age contains
65 and 99.
so here we get person we extract the age
and then we check to make sure that that
contains 65 and 99 so we can check for
the object and then we can actually
check for the values
so if we save this
that should pass
let's say our age was 100
which is wrong
we should fail
so we're expecting 65 and 99 to contain
oops we had 65 and 100 but we could not
find 100.
and that is pretty much a cert J in a
nutshell
so any questions or comments
yeah actually I did have a question
become uh the ass
um operation I know that I've seen that
used but um I myself haven't used it
I've always thought of a use case for it
though uh with reporting
um maybe not at the unit test level but
possibly even at the integration or load
testing whatever uh what are the higher
levels of testing
um using that as operation to couple
into
good job one couple into like x-ray
and you know then you're you know
reporting that stuff out uh in a
specific format
um because you've got the as operation
have you seen anything like that it's
probably uh guilty yes so um okay it's
if you go back to some of my test mg
presentations
um granted those I was not using
um assert J but it's the same practice
uh what you could do is as you create
your unit tests or you create your
individual assertions like in this case
here with the search J you can inject
the description that you want to go with
that specific test uh with the search J
at the actual assertion level which is
awesome because it goes with that fluent
that flow that chaining method where
it's easy to read left to right what's
going on
when you generate the report it's going
to appear on your report in that nice
clean concise way Tess and G allows you
to essentially override that you can do
it both at the unit test or you could
actually do it at the test Suite in in
the XML which is kind of why I like
testing g a little more than uh day unit
plus the fact that you can actually do a
lot more with uh testing G as far as
like system level testing integration
testing and things like that whereas
um like I think you've seen with
integration testing we have to do we
have to kind of budget a little bit to
make it work within uh the unit test
environment
very nice good stuff
thank you any other questions
man how big is this thing is I oh
anytime I'm thinking like Java is up I
feel like it's
pretty much done I'm thinking is this a
new a new thing in Java
no this is a library so think of this
like a plug-in like test NG it is a
library you can add to your project to
enhance your unit testing okay
good questions
I'd like to thank you for your time
you know we spend a lot of time talking
about different topics at developer we
cover different things like testing best
practices you know how to be a better
developer so if you have any ideas or
any topics that you would like us to
discuss or talk through you can reach us
at any of these locations you can email
us at info developmenter.com you can
find us uh reach us through our contact
us page on our main page developer.com
contact us we're also on YouTube
you can find the YouTube link on the
main page of our website I'm it's a very
funky URL so I'm not going to read that
out uh we're also on vimeo.com
developmenter we're still on Facebook at
facebook.com developer and we're on
Twitter at twitter.com developreneur our
goal is making every developer better
have a great day
[Music]
foreign
[Music]
Transcript Segments
10.7

thank you

18.89

[Music]

27.18

hello this is Rob with developmentor

29.519

also known as building better developers

31.619

wanted to announce that we have

34.04

school.developmentor.com feel free to

36.36

check it out if you like any of this

38.52

information any of the content that

40.44

we've sent and you would like to see

41.64

more you can come out you can enroll for

43.739

free we have free courses we've got

46.02

places for you to get better at just

48.66

learning a technology or how to's you

51.3

can work on your business skills we can

53.399

help you with becoming a better

54.78

developer as encoding and things like

57.239

that a lot of the stuff you've seen on

58.92

YouTube we also have out at

61.28

school.developmentor we just have it in

63.239

a little more of a educational format

65.64

and a way for you to track your progress

67.68

as you move forward becoming a better

70.56

developer

72.54

okay next we have collections so now not

76.56

only can we look at strings look at

78.36

objects we can actually take collections

80.64

so I've created a list of person

84.42

uh test people and I'm going to add John

87.299

Smith and Jane Doe to this collection

90.299

now with in this collection we can now

93.36

check to see so as you can see I copied

96.24

the uh

98.88

example from there from the assert.j

102.96

page so in their example they have all

105.96

these uh methods and classes that you

109.14

have to kind of include to see what's

111.78

going on it makes it hard to read

115.259

but here I just went with the simple

117.6

approach so we'll get rid of that line

119.34

so collection

121.619

is specific assertions and there are a

124.979

lot more so we can assert on a

128.819

collection not just a string but on a

131.819

collection we can check things like its

134.94

size we can check to see if it contains

138.12

the objects that we want we can also

140.4

check to see if it doesn't contain the

142.86

objects that we want so this is again

145.08

pretty slick pretty useful and very

148.5

readable we can assert that our test

151.14

people

152.879

our collection is two it contains John

157.98

Smith and Jane Doe and it does not

160.62

contain random people so if I run this

167.04

will pass

168.44

however if I switch this up and I put

172.26

random person here

174.42

okay so to take Jane Doe

179.819

take random person

184.379

now if I run this this should fail

187.08

because test people does not contain

189.66

random person but it does contain Jane

192.42

Doe but let's look at the nice error

194.459

message that we get to basically tell us

196.62

what we did wrong

198.84

so if we come over here

201.659

so Java assert error expecting person

205.14

with John Smith and Jane Doe to contain

208.76

uh John Smith and random person how and

213.78

um

214.44

so we failed at the second criteria

217.019

because it could not find random person

220.2

random person was not in our collection

224.34

which makes it very easy to look at this

226.739

and say ah okay so we asserted to test

228.959

people as to pass but we failed on this

232.739

line we failed because John Smith uh was

236.819

there but random person was not now what

240.12

if I

241.68

add random or change L back

245.04

now we should fail on this line

248.4

so if we run this again

251.939

read our error so again we're expecting

254.76

it writes out the whole collection for

256.799

us this is so slick very helpful so

260.579

we're basically saying okay we've made

262.32

it past the count we made it past the

265.02

expecting so we're expecting uh John

268.919

Smith

269.759

we're expecting Jane Doe but we're

272.94

expecting not to contain Jane Doe

275.94

however it did fine Jane Doe so when we

279.18

go back and look at our go back and look

281.4

at our test we know that we failed on

284.4

this line

286.74

so if I put uh

288.78

revert that back

291.54

now say we

293.16

want to check on this one so it has size

299.82

very easy to read expecting size zero

302.699

but was two and it tells you what was in

305.22

the collection

308.46

so again pretty straightforward fairly

311.04

easy to understand and the nice thing is

314.58

it's so readable it's so easy to follow

317.699

whereas if you just use standard

320.04

assertions again you're going to have to

322.02

create multiple lines of assertions do

325.199

multiple lines of checks and it's going

327.539

to get bloated and very ugly and very

330.38

unreadable very quickly

333.18

right so that's a very simple example of

335.28

using collections

337.5

let's move down to the as

342.539

method so as is used to describe the

346.62

test and will be shown before the error

349.74

message

351.12

so this is neat because now we can even

354.479

override

356.28

at test time what it is that we're doing

360.06

with our test so we can actually name uh

364.08

or describe what our test is doing

365.759

within the test again keeping it with

369.24

that very readable flow that assert J

373.08

gives us with the fluid so we have

375.12

assert that johnsmith.getage

380.699

dot s so we're going all right so we're

383.28

asserting that John Smith's age as we're

386.88

going to check the age

389.639

of John Smith

392.819

is equal to 65. now so we're going to

396.66

check percent s so we're using the

399.72

string

401.72

placeholder or strings and then we're

405.479

going to replace that with the actual

407.759

name from our objects we're going to

409.919

check John Smith's age is equal to 65.

417.84

so now just by reading this it's very

421.44

easy to read so we're going to assert

422.88

that John Smith's age as checking John

426.66

Smith's age is equal to 65.

430.319

now this may seem a little redundant

432.36

here but when we run this

435.06

we're going to see that on our report

439.56

it's very easy to read if it fail so

442.8

right now it passed so we're not going

444.9

to actually see anything but if our age

447.9

was wrong and I ran this

452.639

now we fail

454.259

so now

456.18

our comparison failure check John

458.639

Smith's age expected 64 but was 65. and

463.62

what's really cool is it actually tells

466.08

you or identifies what character was

469.979

different

471.06

so not only does it tell me that 64 is

474.78

not 65 but it actually points out that

477.66

it's the second the first number I'm

480.72

sorry the single unit number that was

483.72

different

485.039

again making this very easy to debug and

488.28

very easy to follow

490.56

all right so we'll put that back

494.099

so that is the as so that again is to

498.06

describe the test that you're doing

502.08

right so next say we want to test some

505.86

assertions

507.72

this one is straight from their example

510

I thought it was pretty clean very easy

512.94

to follow so I didn't want to go too far

515.82

off the rails so there's two ways we

518.159

could do this

520.26

so we can assert that

523.44

uh we can assert that we're going to get

526.2

an exception so we're going to assert

527.82

that a throne by so instead asserting

530.94

that

532.32

some check some pass is going to work

534.779

we're going to assert a throne we're

537.839

going to assert a uh an exception is

540.839

going to happen

542.04

then we're going to use Lambda and we're

545.94

going to essentially throw the assertion

548.82

and the message that we want

551.279

now

552.779

this is just a straight example we're

555.42

forcing the assertion we could

558.42

call John Smith's age or we could call

561.899

our calculator and we could trigger

564.3

whatever the action is that creates the

566.76

exception and then we can verify that

569.82

the message was thrown by using has

572.1

message

573.24

so to read this it is assert that thrown

577.08

by will happen when some execution

581.399

occurs and then we want to make sure

584.16

that we receive the message from the

586.74

exception

587.76

now if we wanted to use a little bit

590.16

more Behavior driven uh

592.92

testing style we can do it this way we

595.74

can do throwable Throne equals catch

599.22

throwable

600.839

again Lambda again using the throne new

604.2

exception and then we can assert that

607.2

Throne so we can assert that the

609.74

exception has message containing boom

614.76

so you could do either I actually like

618.3

this a little bit more but it does

621.06

require you to actually do the uh or

625.22

separate the layer between the exception

628.86

and the assert that so you could do it

631.44

this way which is everything's wrapped

633.54

together which is very clean or you can

636.3

do it this way where you actually

638.16

break the logic that creates the

640.14

exception and then you test the

642

exception

643.14

both ways work uh in both ways uh

647.1

generate the same message

650.04

so if I wanted to run this again this

653.16

will pass

656.339

and if we want to break it

661.92

and run

666.06

and expand this

668.459

so we see that the expected message was

672.24

supposed to be Boom at but we received

675.66

boom exclamation point

677.519

again exception assertion test arraylist

681.54

exception and assertion error now if I

684.959

put this one back

688.2

now it should fail on the second one

693.24

and as you see it is the exact same

697.079

message so again either approach Works

700.56

uh you get the same result

703.32

next let's look at extracting assertions

707.16

so here is another useful way we can use

712.38

Collections and assertions

716.519

so here I'm again going to create a

720.18

collection of persons I'm going to go

723.779

ahead and add all three people random

726

John Smith and Jane Doe

728.279

what's cool about this is using the

731.22

assert that method again we can pass in

734.7

a collection now not just an object and

738.36

then we can use this extracting method

741.18

to actually extract an individual class

744.62

from within our collection so I can take

747.839

person get name

749.82

and I'm not even just checking the class

752.459

I can actually check all the objects in

756.36

this collection based on their

760.26

um get name so I can base it on their on

764.22

a method so person get name

767.04

then I can use other comparative methods

770.94

like contains does not contain string

773.459

equal

774.6

and I can pass in a list of values to

779.579

check so I can say it does not contain

781.38

random does not contain person

785.279

another way of doing this we can

787.38

actually extract so this is for checking

789.899

a single value

791.94

with multiple checks on

795.06

uh get name

796.74

but we could also check multiple

799.56

so I could actually extract again

803.1

but this time not only am I extracting

806.16

get name but I can actually get name

808.8

page and State

810.959

so I don't even have to just use get

812.639

name I can actually use the variables

816.779

and then I can check to see if it

818.94

contains and this is cool where I can

821.399

use the Tuple method

824.76

which allows it's a utility method to

827.339

build a tuple basically you can

831.18

kind of chain out what it is that your

833.94

the values to match what it is you're

837.06

extracting so I can essentially create a

839.82

mock object with random person for name

843.019

page and State

846.36

all right so let me save this

850.26

run this

854.04

and this will pass

856.079

all right so let's take the first part

857.94

here

858.779

so random person so we're going to

861.48

assert that to uh the first one here

863.42

extracting person does not contain

866.279

random or person

869.76

so this time I'm actually going to look

873.3

for the whole name random person

876.06

so if I run this this should fail

878.22

because random person is in the list and

882.48

it does

883.98

it fails expecting random person John's

888.54

uh so it's expecting so the collection

891.06

I'm passing in is random person John

893.1

Smith Jane Doe not to contain random

896.22

person however it failed because random

899.1

person was in the list

902.16

again I am not writing any of these uh

908.1

uh I'm not writing any of these test

910.079

cases I'm not running any of this logic

912

this is all built into a search day

915.42

foreign

920.839

person and say we want to test the

923.639

second part here

925.56

and let's have Jane Doe fail we'll set

929.16

her age to be a hundred

931.68

run that again

934.62

we failed so we were expecting a

938.459

collection uh random person age state

943.199

uh John Smith Gage State Jane Doe H

946.86

state to contain random person 12. okay

950.579

that one looks good John Smith 65

953.279

Florida that looks good and whoops uh

956.94

we're looking for Jane Doe 100 that's

959.519

not there and it couldn't find them

962.519

so again very easy

965.16

to follow very easy

967.86

failures

969.42

okay and the last one we're going to

971.399

look at today is filtering a collection

976.8

so not only can you pull out values

978.899

within the collection you can actually

981.18

filter on the collection

983.399

so here and

986.279

spaces

989.699

all right so now we're going to take

992.16

that same collection that we used

993.54

previously

994.62

but we're going to filter on the

997.019

collection so we're going to assert that

999.66

our collection test people we are going

1003.199

to filter on a character

1008.839

in the name so a really character in

1011.54

this case is person so we want person

1015.62

so I'll change that to person

1021.44

I like being descriptive so we have our

1024.14

person object

1025.579

person

1027.14

so filter on

1029.839

person dot get name dot contains J

1034.22

so we're going to look for

1036.86

all names so it's going to return me all

1041.12

the names

1042.98

or all the objects in the collection

1046.12

that contain the name

1049.82

with the letter j capital j so

1053.12

this is going to return us should return

1055.34

us just John Smith and Jane Doe

1058.22

which we then can do that contains only

1060.559

John Smith and Jane Doe

1065

and this should pass so if I run this

1070.34

we pass

1071.66

however if I look for a random person

1075.14

random person does not have J in the

1078.44

name and this guy should fit

1082.7

and it does so we're expecting person

1087.44

John Smith Jane Doe to contain only

1092.24

person John Smith Jane Doe random person

1095.66

but could not find the following

1097.46

elements person random person so we're

1100.94

looking for that J

1103.16

all right so we go back

1105.86

and again so it contains only and then

1108.02

we can do contains

1111.26

we can do all

1113.6

we can do exactly

1117.2

we can say null

1122.059

and we can even check sequence so

1124.039

there's a lot of different things we can

1125.179

do with this which is really cool

1127.4

right so here again we're doing person

1131.72

name dot contains J

1135.38

contains only John Smith

1138.5

all right and the last one we're going

1140.539

to look at is combining filtering and

1143.179

extracting so now we can actually it

1145.82

just shows you that you can do more you

1148.1

can do it pretty much anything once you

1150.799

start this the stream once you start the

1153.26

fluent path of assert that so here we're

1156.26

going to start that test people

1159.08

we're going to filter like we did up

1161.48

here

1163.16

again on person

1169.58

and we're going to do person.name

1175.039

contains capital j

1177.559

okay filter containing only

1180.86

again John Smith Jane Doe does not

1183.62

matter on the order

1187.16

so up to this point we are doing exactly

1189.799

what we just did in the first part so

1192.14

we're going to assert that the test

1194.419

people with capital J in the name should

1198.62

contain Jane Doe and John Smith

1201.86

however we can go further we can then

1204.86

once we get these two objects

1209.66

we can extract person

1212.48

then we can extract their age and then

1215.36

we can double check to make sure that

1217.039

the age contains

1221.24

65 and 99.

1224.24

so here we get person we extract the age

1227.059

and then we check to make sure that that

1229.039

contains 65 and 99 so we can check for

1234.799

the object and then we can actually

1237.26

check for the values

1240.02

so if we save this

1243.86

that should pass

1245.84

let's say our age was 100

1249.08

which is wrong

1250.58

we should fail

1253.1

so we're expecting 65 and 99 to contain

1256.16

oops we had 65 and 100 but we could not

1259.52

find 100.

1261.26

and that is pretty much a cert J in a

1264.14

nutshell

1265.46

so any questions or comments

1270.02

yeah actually I did have a question

1272.36

become uh the ass

1275.12

um operation I know that I've seen that

1277.46

used but um I myself haven't used it

1280.28

I've always thought of a use case for it

1282.559

though uh with reporting

1285.2

um maybe not at the unit test level but

1288.919

possibly even at the integration or load

1292.22

testing whatever uh what are the higher

1294.32

levels of testing

1295.88

um using that as operation to couple

1300.2

into

1301.88

good job one couple into like x-ray

1305.419

and you know then you're you know

1307.1

reporting that stuff out uh in a

1309.26

specific format

1310.82

um because you've got the as operation

1312.38

have you seen anything like that it's

1314.419

probably uh guilty yes so um okay it's

1319.64

if you go back to some of my test mg

1322.159

presentations

1324.559

um granted those I was not using

1327.98

um assert J but it's the same practice

1331.419

uh what you could do is as you create

1335.36

your unit tests or you create your

1337.46

individual assertions like in this case

1339.5

here with the search J you can inject

1342.559

the description that you want to go with

1345.38

that specific test uh with the search J

1349.22

at the actual assertion level which is

1351.919

awesome because it goes with that fluent

1355.28

that flow that chaining method where

1357.2

it's easy to read left to right what's

1359.48

going on

1361.1

when you generate the report it's going

1363.62

to appear on your report in that nice

1365.78

clean concise way Tess and G allows you

1369.32

to essentially override that you can do

1372.2

it both at the unit test or you could

1374.9

actually do it at the test Suite in in

1377.9

the XML which is kind of why I like

1379.7

testing g a little more than uh day unit

1383.84

plus the fact that you can actually do a

1386.36

lot more with uh testing G as far as

1389.179

like system level testing integration

1391.28

testing and things like that whereas

1393.98

um like I think you've seen with

1395.72

integration testing we have to do we

1398.24

have to kind of budget a little bit to

1400.76

make it work within uh the unit test

1403.52

environment

1405.32

very nice good stuff

1408.14

thank you any other questions

1410.419

man how big is this thing is I oh

1412.7

anytime I'm thinking like Java is up I

1415.159

feel like it's

1416.419

pretty much done I'm thinking is this a

1418.46

new a new thing in Java

1421.34

no this is a library so think of this

1424.4

like a plug-in like test NG it is a

1427.4

library you can add to your project to

1430.22

enhance your unit testing okay

1434.24

good questions

1436.88

I'd like to thank you for your time

1440.179

you know we spend a lot of time talking

1442.64

about different topics at developer we

1445.64

cover different things like testing best

1448.159

practices you know how to be a better

1450.5

developer so if you have any ideas or

1454.76

any topics that you would like us to

1456.919

discuss or talk through you can reach us

1459.919

at any of these locations you can email

1462.14

us at info developmenter.com you can

1465.2

find us uh reach us through our contact

1467.12

us page on our main page developer.com

1469.88

contact us we're also on YouTube

1473.9

you can find the YouTube link on the

1476.12

main page of our website I'm it's a very

1479.36

funky URL so I'm not going to read that

1481.039

out uh we're also on vimeo.com

1483.52

developmenter we're still on Facebook at

1486.32

facebook.com developer and we're on

1489.559

Twitter at twitter.com developreneur our

1492.62

goal is making every developer better

1494.299

have a great day

1501.37

[Music]

1510.26

foreign

1511.93

[Music]