📺 Develpreneur YouTube Episode

Video + transcript

AssertJ Part1

2023-01-17 •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
foreign
[Music]
[Music]
so welcome to another edition of
developer today we are going to be
talking about a certain J packages and
it is a fluent assertions in Java so
instead of us using the traditional
junit assert class we're going to be
talking about this new library assert J
So today we're going to be looking at
unit testing with the cert J we're going
to kind of briefly talk about the
differences between or what assertions
are and why they're important within
unit testing and then we're going to go
into a full Hands-On presentation using
assert J methods and chaining within our
unit testing
so first let's talk about unit testing
so unit testing is software testing
where we're testing the individual units
of our source code so we're testing
individual methods we're testing units
of work we're not testing the overall
application we're not testing we're not
doing regression testing we're not doing
integration testing within our
application and other applications this
is solely focused on a specific unit a
potentially a method in our code
why is unit testing important well it
helps us to identify problems early in
the development cycle this lets US
Weekly 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 lower everything passed and we
can then move on
finding bugs and fixing bugs at this
level is very cost effective because
we're able to fix it now whereas if we
waited till we did regression testing or
manual testing of the application once
it's been packaged and shipped it's
going to be harder for us to identify
what that bug is or where that problem
came from so if we do unit testing right
we should be able to catch most of the
functionality bugs within our
application quickly during development
it also makes debugging the process
easier because if we catch the bug in
our unit test we know specifically in
our code where it broke we know where to
go fix the problem again if we wait till
the end when we're trying to package and
Chip the product we don't necessarily
know where the bug exists so it's going
to 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 also reuse our code to migrate
our specific test from say one
application to another so if we have
similar code in one application we could
potentially copy that unit test put it
into the other code base make a couple
of tweaks and boom we're done
so unit testing very effective this is
the first line of testing that
developers can do while they're writing
their source code
now this leads into assertions
what are assertions well the Oxford
dictionary defines assertions is a
confident and forceful statement of fact
or belief
which really translates into what we're
looking at when we talk about testing
so from the junit or unit testing
perspective and assertion is essentially
a collection of utility methods that
support asserting conditions in tests so
it's like given some test scenario when
we give it some data then we want to
test something we want to make sure that
things work again assertions is a
competent and forceful statement of fact
or belief
assert J is a third party library that
you can include in your project that is
Rich and easy to use it is extensible
it's open source so you can go extend
the libraries it is also Community
Driven so there are no licenses or costs
to using this Library
assert J provides a rich set of
assertions
truly helpful error messaging improves
test code readability and is designed to
be super easy to use within your
favorite IDE
and the whole reason I'm even talking
about the search J today is because once
I found this and I started using this
this really makes writing those unit
testings and writing those a search
writing those use cases it makes it so
much more readable and easy to follow in
your use cases than trying to create a
whole bunch of independent asserts
with the traditional assert libraries
that come with junit or some of the
other unit libraries
so that's pretty much it for the slides
so we're going to jump out we're going
to look at the different apis between
junit is searching and assert J and then
we're going to walk through a simple use
test on how to use some of the
functionalities that come with the
search J
all right so let's first start with
assertions
in junit the assertion Library contains
a whole bunch of helper methods that we
can use to define assertions within our
use cases we can do things like assert
all we can assert arrays are equal to
things
but basically there is an assert method
for every type of test that you want to
do but you have to essentially assert
for everything you want to do so either
you create if statements around
conditions or you have to basically
Define multiple assertion statements in
order for you to test your library so
you have like a search equals
a certain not equals a certain null
which is really useful in and of itself
like this are true fail uh but we don't
necessarily want to have to have 5 10 15
asserts to essentially test a use case
it gets to be unreadable unmanageable
however with assert J assert J uses
fluence so you can use method chaining
starting with and assert that
instead of assert equals so you
basically start with the Declaration
assert that and then you can use fluent
and basically chain any of the methods
within the assert J which is very useful
so the two locations to look at if you
want to compare these if if you go to
junit.org junit Five docs and the
version you want then API
index.html will bring up the junit API
of your choice assert J you can get to
by just Googling assert J and pick
whichever index you want but they are on
GitHub it is a weird URL it is Joel Dash
castigila Lego uh steliago anyway it's
easier if you just Googled assert day go
to their GitHub and then you can find
the apis there
go back to our code
so we're going to start with a simple
test here
so let's look at our project here so our
project is a basic Maven project I just
named it a search examples for clarity
for this class
and I added two classes to my Java main
packet so I have a calculator class from
last time which has all of our methods
for adding subtracting dividing
multiplying basically the basic
arithmetic functionalities for a
calculator
we have factorial and then we had our
main class where we actually did the
operations where we took command line
arguments and actually added up or
tested our methods so to start with I
want to introduce instead of just
jumping right into search a I want to
show you how to write a unit test to
test
protected private methods within a class
so we'll start with this one and then
we'll move into testing our second class
here which is a basic pojo a person with
a name address city state ZIP so down in
Source test Java I've created a
calculator test class
and we will ignore this for right now
so in our calculator test it is a junit
5 test so I'm using junit Jupiter API
test
which allows me to use the test
annotations
and then I create a method to test the
add to numbers method from my calculator
so we want to test this right here we
want to test this method
but this method is protected we it's not
public so I can't just say calculator
add two numbers and go on
so to access a method inside of a class
that is protected or private you can use
the method reflection class here Java
link reflect method
and then we can go in and we can take
the class name we want dot class we can
use the get declared method for the
method reflection we then can specify
the method that we want
so essentially we're defining the method
within our test so we're going to say
okay get declared method we want to get
the method name add so basically you
kind of copy this here so you get the
method name add two numbers and then you
also want to set the parameter types
that you're going to have to pass in
so we give it the method name and then
we give it the two parameter types that
we want to pass in which our flow
the next thing we want to do is we want
to take our method reflection and we
want to set it accessible so we want to
set it true that way we can access that
private protected method
and then you can just simply use a
standard assert this is not a search
eight this is just a standard assert
method where we can assert equals that
the result of adding two numbers one and
one is going to be 2 we have to add the
F postfix so that we can Define this
decimal as a float we call our method
add two numbers returns result method
invoke so we're going to invoke it we're
just going to say a okay call it
we Define our new calculator so we
instantiate our calculator class and
then we pass in two parameter values of
one and one which should equal two so if
we run this we should pass now if this
failed it would throw the message the
sum should be 2. all right so let's run
this
and over here if I extend this we pass
but if I change this to 2 and rerun this
we fail and if we expand this
you'll see that the message here that we
added was written out the sum should be
2 expected to but we received 3.
so this is an example of using a
standard assert method within our test
case
now if we want to use assert J we need
to import some assert J classes here so
we need to include org dot assert J dot
core dot API Dot assertions
now you could do dot star but you know
in the standard world today we only want
to include the classes that we want so
for today's example we're going to be
looking at the assert that method which
is what we're going to start all of our
assertions with we're going to look at a
cert that throws by to actually test
exceptions so we can actually kind of
force an exception or a method that may
throw an exception
catch throwable and Tuple which will
allow us to go in and actually check
different values within a object which
is very useful
all right so let me go down here
add my test data back in
so for our
uh sir J Test we are going to use our
person class so we're going to use our
person pojo to test some data that is
being passed around
I kind of followed the example that they
used on their website I just kind of
simplified it a little bit further
without having to include all the
additional classes they used for testing
before we get to that point though you
will need to also include the dependency
assert J so your Maven dependency will
be group ID or assert J
the artifact you need is the search J
dash core
and currently the version uh that's out
there right now is 3.4.1 and I want to
set the scope to test so that when I
build the project it will not include
the test packages within my compiler
so test one let's look at a basic
assertion test so this is going to kind
of be a very simple test kind of like
what we did here with the assert equals
so here I've created three persons or I
could find three persons for testing
I've created a random person John Smith
and Jane Doe all with some uh different
person
data that we're going to test
so for our first test method our basic
assertions we're going to follow the
chaining method the fluent method of a
search a so we begin our test with
assert that
and then we give it
our uh what we're going to test so
assert that here random person dot get
name
so assert
what it is we're checking
then
what do we want to test so we have
assert that so let's look at this so
assert
that
can receive a string
but if we go look over here
so if we look at assert that
you see the assert that takes it's been
overloaded to handle multiple
different types so we can take files
floats just about everything possible
within the Java language we can take
almost any type of class
so we can uh pass in anything so say we
want to insert a string
is equal to another string
so we start with the string
then you can do Dot and now you have all
your
assertion or certain J methods that you
can use so you can check for
um we'll talk about as in a second but
we'll stick to Strings so as string
contains contains only contains all
patterns so you have a whole slew of
methods now that you can test
uh within this assert that
method
whereas if we just did the straight up
assert equals or assert true this is a
one and done you you make this method
call and that's it
so if you want to do another test or
chain off of this test you have to
create multiple assertions
however here with the assert that it
allows you to chain this test so not
only can I assert that random
person.getname is equal to random person
I can also continue chaining off of the
last method so I can say okay is this
equal to random person okay cool is it
also
um equal to ignored case uh so you could
actually essentially create your one
test or combine all the things you're
testing into one statement whereas again
with the assert you would have to create
multiple asserts uh that you would have
to test so if one failed you would have
to then have to go up okay this and
figure out what failed whereas here if
you change it all into one command if
this guy fails you know that this is the
line you need to go look at
all right so let's start with the first
part here so how we read this assert
debt random person.getname is equal to
random person
very readable now if we wanted to do
this up here it would be something like
this so uh straight up assertion would
be assert equals
random person get name
random person
so assert equals random person get name
random person
still fairly readable but this reads
better assert that random person get
named is equal to random person
a certain equals random person name
random person doesn't quite roll off the
tongue as easily the other thing is okay
so if I did this but then I wanted to do
some additional things off of this I
would have to do another assert and
continue whereas here if I want to do
another dot is
equal
uh ignore case I could actually add some
other chaining here if I wanted to
where here I would have to do yet
another assert
all right so there's the first two
or sorry first one and then the second
one
not only can we check by string but we
can actually actually check by object
type so I can say assert that random
person
is not equal to John Smith
if we go up here so random person should
be this person object and John Smith
will be this object so it actually
allows you to compare objects as well
so if I save this and run
our test pass
now if I actually put in random person
this should fail because random person
is equal to random person
and sure enough it failed
so let's look at this so our assert
error it's expecting a person name
random person address one two three
is not uh to be equal to random person
so
we fail
another thing I like about assert J is
the failure messages you get at runtime
are so much easier to understand and
they're clean they're easy to read uh
unlike a certain methods you would have
to kind of expand upon the assert
methods like so
okay so now we have this one put it back
so it passes
so that's just basic assertion so that's
very simple comparison between the two
so now let's look at this chaining
method so the second one here is
chaining
so
chaining allows us to chain different
things we can change strings
specifically we contain collections but
for this one let's just start with
string
so here we're going to assert that
[Music]
johnsmith.getname dot starts with John
we can also check that it ends with th
and we can see if it is equal to ignored
case
John Smith
again if we wanted to do this as assert
you would have to do something like this
so here we have to essentially write
three assert methods to do the same
thing that we can do in a search J in
one line
so here we can assert that John Smith
dot get name starts with joh DOT ends
with th dot equals ignored case John
Smith
a lot easier to read and very easy to
understand where this gets convoluted or
if you have a lot of these it's going to
be hard to tell what exactly the test is
doing
okay
put those go back to clean
run this again
so if we look at our chaining it passed
now let's make it fail
so let's say we did John by mistake
the first part should fail that's passed
that should still pass but the first
part failed
foreign
and if we look we were expecting John
Smith to start with John and it failed
because
um our name was actually j-o-h-n
so it did not start with John so it
failed
very clean very easy to read
okay put that back together H so this
passes
all right so that's chaining
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 our 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.develop anymore we just have it a
little more of a educational format and
a way for you to track your progress as
you move forward becoming a better
developer
foreign
[Music]
Transcript Segments
0

foreign

5.11

[Music]

15.71

[Music]

20.039

so welcome to another edition of

23.279

developer today we are going to be

26.4

talking about a certain J packages and

30.06

it is a fluent assertions in Java so

33.6

instead of us using the traditional

36.3

junit assert class we're going to be

38.52

talking about this new library assert J

42.48

So today we're going to be looking at

45.18

unit testing with the cert J we're going

47.46

to kind of briefly talk about the

49.32

differences between or what assertions

51.539

are and why they're important within

54.12

unit testing and then we're going to go

56.28

into a full Hands-On presentation using

59.46

assert J methods and chaining within our

63

unit testing

64.979

so first let's talk about unit testing

66.96

so unit testing is software testing

69.78

where we're testing the individual units

73.32

of our source code so we're testing

75.06

individual methods we're testing units

77.64

of work we're not testing the overall

80.28

application we're not testing we're not

82.439

doing regression testing we're not doing

83.939

integration testing within our

85.439

application and other applications this

87.84

is solely focused on a specific unit a

92

potentially a method in our code

95.22

why is unit testing important well it

97.619

helps us to identify problems early in

100.02

the development cycle this lets US

102.119

Weekly catch bugs or issues with our

105.9

code changes at development time so as

109.38

we're writing the code we can test the

111

code and say oops our change broke

112.92

something lower everything passed and we

115.32

can then move on

117.18

finding bugs and fixing bugs at this

119.28

level is very cost effective because

122.22

we're able to fix it now whereas if we

125.82

waited till we did regression testing or

127.979

manual testing of the application once

129.899

it's been packaged and shipped it's

132.12

going to be harder for us to identify

134.04

what that bug is or where that problem

136.56

came from so if we do unit testing right

139.56

we should be able to catch most of the

141.9

functionality bugs within our

143.64

application quickly during development

146.099

it also makes debugging the process

148.2

easier because if we catch the bug in

151.08

our unit test we know specifically in

153.66

our code where it broke we know where to

155.58

go fix the problem again if we wait till

158.16

the end when we're trying to package and

160.62

Chip the product we don't necessarily

162.42

know where the bug exists so it's going

164.4

to take more time and effort to go back

166.14

in and dig into the code and find the

168.36

problem

169.379

the other nice thing about unit testing

171.239

is we can also reuse our code to migrate

176.16

our specific test from say one

178.2

application to another so if we have

179.76

similar code in one application we could

182.58

potentially copy that unit test put it

185.22

into the other code base make a couple

186.9

of tweaks and boom we're done

189.42

so unit testing very effective this is

192.48

the first line of testing that

193.92

developers can do while they're writing

195.959

their source code

197.819

now this leads into assertions

201

what are assertions well the Oxford

203.459

dictionary defines assertions is a

207.12

confident and forceful statement of fact

209.64

or belief

211.08

which really translates into what we're

213.959

looking at when we talk about testing

216.659

so from the junit or unit testing

219.9

perspective and assertion is essentially

222.48

a collection of utility methods that

224.94

support asserting conditions in tests so

228.72

it's like given some test scenario when

232.44

we give it some data then we want to

235.5

test something we want to make sure that

237

things work again assertions is a

239.94

competent and forceful statement of fact

241.739

or belief

243.9

assert J is a third party library that

247.62

you can include in your project that is

250.14

Rich and easy to use it is extensible

253.5

it's open source so you can go extend

256.199

the libraries it is also Community

259.079

Driven so there are no licenses or costs

261.9

to using this Library

264.24

assert J provides a rich set of

267

assertions

268.259

truly helpful error messaging improves

271.56

test code readability and is designed to

274.08

be super easy to use within your

275.699

favorite IDE

277.139

and the whole reason I'm even talking

279.419

about the search J today is because once

282.78

I found this and I started using this

285.12

this really makes writing those unit

287.88

testings and writing those a search

290.1

writing those use cases it makes it so

293.04

much more readable and easy to follow in

296.46

your use cases than trying to create a

298.979

whole bunch of independent asserts

301.68

with the traditional assert libraries

304.68

that come with junit or some of the

306.9

other unit libraries

309.6

so that's pretty much it for the slides

311.759

so we're going to jump out we're going

313.62

to look at the different apis between

316.38

junit is searching and assert J and then

319.56

we're going to walk through a simple use

322.38

test on how to use some of the

325.02

functionalities that come with the

326.4

search J

328.08

all right so let's first start with

330.72

assertions

331.979

in junit the assertion Library contains

335.88

a whole bunch of helper methods that we

338.16

can use to define assertions within our

342.36

use cases we can do things like assert

344.639

all we can assert arrays are equal to

347.52

things

348.72

but basically there is an assert method

351.12

for every type of test that you want to

353.46

do but you have to essentially assert

356.52

for everything you want to do so either

358.86

you create if statements around

360.72

conditions or you have to basically

363.539

Define multiple assertion statements in

367.139

order for you to test your library so

369.36

you have like a search equals

371.52

a certain not equals a certain null

374.82

which is really useful in and of itself

378.979

like this are true fail uh but we don't

383.88

necessarily want to have to have 5 10 15

388.46

asserts to essentially test a use case

391.5

it gets to be unreadable unmanageable

394.319

however with assert J assert J uses

398.22

fluence so you can use method chaining

401.759

starting with and assert that

405.18

instead of assert equals so you

408.06

basically start with the Declaration

410.16

assert that and then you can use fluent

412.86

and basically chain any of the methods

416.28

within the assert J which is very useful

420

so the two locations to look at if you

423.479

want to compare these if if you go to

425.36

junit.org junit Five docs and the

429.539

version you want then API

432.56

index.html will bring up the junit API

436.199

of your choice assert J you can get to

440.34

by just Googling assert J and pick

442.919

whichever index you want but they are on

446.099

GitHub it is a weird URL it is Joel Dash

450.8

castigila Lego uh steliago anyway it's

455.759

easier if you just Googled assert day go

459

to their GitHub and then you can find

461.099

the apis there

462.539

go back to our code

464.819

so we're going to start with a simple

468.36

test here

469.8

so let's look at our project here so our

472.5

project is a basic Maven project I just

476.22

named it a search examples for clarity

478.979

for this class

480.419

and I added two classes to my Java main

485.16

packet so I have a calculator class from

488.34

last time which has all of our methods

492.72

for adding subtracting dividing

496.34

multiplying basically the basic

498.9

arithmetic functionalities for a

500.759

calculator

501.78

we have factorial and then we had our

504.419

main class where we actually did the

506.46

operations where we took command line

508.379

arguments and actually added up or

510.479

tested our methods so to start with I

514.14

want to introduce instead of just

515.88

jumping right into search a I want to

517.56

show you how to write a unit test to

520.919

test

522.06

protected private methods within a class

524.58

so we'll start with this one and then

525.959

we'll move into testing our second class

528.18

here which is a basic pojo a person with

531.839

a name address city state ZIP so down in

535.32

Source test Java I've created a

537.3

calculator test class

539.519

and we will ignore this for right now

543.72

so in our calculator test it is a junit

547.68

5 test so I'm using junit Jupiter API

550.38

test

551.7

which allows me to use the test

553.44

annotations

554.94

and then I create a method to test the

557.82

add to numbers method from my calculator

560.42

so we want to test this right here we

564.12

want to test this method

566.04

but this method is protected we it's not

569.459

public so I can't just say calculator

571.76

add two numbers and go on

574.74

so to access a method inside of a class

579.24

that is protected or private you can use

582.66

the method reflection class here Java

585.72

link reflect method

587.58

and then we can go in and we can take

590.339

the class name we want dot class we can

593.7

use the get declared method for the

597.18

method reflection we then can specify

599.82

the method that we want

602.519

so essentially we're defining the method

605.1

within our test so we're going to say

607.019

okay get declared method we want to get

609.42

the method name add so basically you

612.54

kind of copy this here so you get the

614.22

method name add two numbers and then you

617.399

also want to set the parameter types

621.12

that you're going to have to pass in

623.04

so we give it the method name and then

625.74

we give it the two parameter types that

627.66

we want to pass in which our flow

629.64

the next thing we want to do is we want

631.68

to take our method reflection and we

634.56

want to set it accessible so we want to

637.019

set it true that way we can access that

639.959

private protected method

642.36

and then you can just simply use a

644.339

standard assert this is not a search

646.68

eight this is just a standard assert

648.6

method where we can assert equals that

652.56

the result of adding two numbers one and

656.579

one is going to be 2 we have to add the

660.24

F postfix so that we can Define this

662.579

decimal as a float we call our method

665.22

add two numbers returns result method

668.48

invoke so we're going to invoke it we're

671.459

just going to say a okay call it

674.22

we Define our new calculator so we

677.339

instantiate our calculator class and

679.8

then we pass in two parameter values of

681.72

one and one which should equal two so if

684.6

we run this we should pass now if this

687.779

failed it would throw the message the

689.94

sum should be 2. all right so let's run

692.82

this

694.279

and over here if I extend this we pass

699

but if I change this to 2 and rerun this

704.82

we fail and if we expand this

708.42

you'll see that the message here that we

710.519

added was written out the sum should be

713.16

2 expected to but we received 3.

718.38

so this is an example of using a

721.019

standard assert method within our test

724.56

case

725.7

now if we want to use assert J we need

729.48

to import some assert J classes here so

732.72

we need to include org dot assert J dot

736.86

core dot API Dot assertions

739.8

now you could do dot star but you know

742.56

in the standard world today we only want

746.04

to include the classes that we want so

748.8

for today's example we're going to be

751.019

looking at the assert that method which

753.6

is what we're going to start all of our

755.88

assertions with we're going to look at a

758.7

cert that throws by to actually test

761.64

exceptions so we can actually kind of

763.56

force an exception or a method that may

766.139

throw an exception

767.88

catch throwable and Tuple which will

771.48

allow us to go in and actually check

774.72

different values within a object which

778.92

is very useful

780.36

all right so let me go down here

784.2

add my test data back in

787.26

so for our

789.06

uh sir J Test we are going to use our

793.139

person class so we're going to use our

794.519

person pojo to test some data that is

797.04

being passed around

799.019

I kind of followed the example that they

801.839

used on their website I just kind of

804.72

simplified it a little bit further

806.16

without having to include all the

808.32

additional classes they used for testing

811.62

before we get to that point though you

813.959

will need to also include the dependency

817.1

assert J so your Maven dependency will

821.1

be group ID or assert J

824.279

the artifact you need is the search J

826.68

dash core

827.839

and currently the version uh that's out

830.519

there right now is 3.4.1 and I want to

833.7

set the scope to test so that when I

836.82

build the project it will not include

839.1

the test packages within my compiler

843.3

so test one let's look at a basic

846.48

assertion test so this is going to kind

848.82

of be a very simple test kind of like

851.339

what we did here with the assert equals

854.76

so here I've created three persons or I

858.779

could find three persons for testing

860.339

I've created a random person John Smith

862.92

and Jane Doe all with some uh different

865.62

person

867.3

data that we're going to test

869.639

so for our first test method our basic

872.639

assertions we're going to follow the

875.579

chaining method the fluent method of a

878.1

search a so we begin our test with

880.68

assert that

882.36

and then we give it

884.519

our uh what we're going to test so

888.3

assert that here random person dot get

892.8

name

894

so assert

895.86

what it is we're checking

898.62

then

900.18

what do we want to test so we have

902.76

assert that so let's look at this so

905.88

assert

907.139

that

908.6

can receive a string

912.06

but if we go look over here

916.8

so if we look at assert that

919.26

you see the assert that takes it's been

922.92

overloaded to handle multiple

925.8

different types so we can take files

928.079

floats just about everything possible

930.199

within the Java language we can take

933.12

almost any type of class

935.94

so we can uh pass in anything so say we

939.3

want to insert a string

941.399

is equal to another string

943.8

so we start with the string

946.74

then you can do Dot and now you have all

951

your

951.92

assertion or certain J methods that you

954.66

can use so you can check for

956.82

um we'll talk about as in a second but

959.339

we'll stick to Strings so as string

962.3

contains contains only contains all

965.839

patterns so you have a whole slew of

969.12

methods now that you can test

972

uh within this assert that

975.36

method

977.16

whereas if we just did the straight up

979.26

assert equals or assert true this is a

982.86

one and done you you make this method

985.139

call and that's it

987.24

so if you want to do another test or

989.94

chain off of this test you have to

991.92

create multiple assertions

994.74

however here with the assert that it

997.68

allows you to chain this test so not

1001.16

only can I assert that random

1003.759

person.getname is equal to random person

1006.139

I can also continue chaining off of the

1010.579

last method so I can say okay is this

1013.279

equal to random person okay cool is it

1016.639

also

1018.259

um equal to ignored case uh so you could

1022.459

actually essentially create your one

1025.4

test or combine all the things you're

1028.04

testing into one statement whereas again

1031.28

with the assert you would have to create

1032.9

multiple asserts uh that you would have

1036.679

to test so if one failed you would have

1038.9

to then have to go up okay this and

1040.88

figure out what failed whereas here if

1043.4

you change it all into one command if

1045.38

this guy fails you know that this is the

1047.54

line you need to go look at

1050.299

all right so let's start with the first

1052.64

part here so how we read this assert

1055.94

debt random person.getname is equal to

1059.179

random person

1060.86

very readable now if we wanted to do

1063.5

this up here it would be something like

1065.6

this so uh straight up assertion would

1068.36

be assert equals

1075.38

random person get name

1083.96

random person

1085.7

so assert equals random person get name

1089.059

random person

1090.38

still fairly readable but this reads

1093.679

better assert that random person get

1095.72

named is equal to random person

1098.6

a certain equals random person name

1100.46

random person doesn't quite roll off the

1102.32

tongue as easily the other thing is okay

1105.32

so if I did this but then I wanted to do

1107.6

some additional things off of this I

1109.46

would have to do another assert and

1111.98

continue whereas here if I want to do

1114.08

another dot is

1116.12

equal

1118.28

uh ignore case I could actually add some

1121.16

other chaining here if I wanted to

1123.38

where here I would have to do yet

1125.059

another assert

1128.74

all right so there's the first two

1132.679

or sorry first one and then the second

1135.14

one

1136.76

not only can we check by string but we

1139.94

can actually actually check by object

1143.36

type so I can say assert that random

1145.82

person

1146.96

is not equal to John Smith

1150.32

if we go up here so random person should

1152.84

be this person object and John Smith

1155.539

will be this object so it actually

1157.82

allows you to compare objects as well

1161

so if I save this and run

1166.52

our test pass

1168.62

now if I actually put in random person

1173.419

this should fail because random person

1175.88

is equal to random person

1182.66

and sure enough it failed

1185.539

so let's look at this so our assert

1187.94

error it's expecting a person name

1190.52

random person address one two three

1193.76

is not uh to be equal to random person

1197.6

so

1199.94

we fail

1201.559

another thing I like about assert J is

1204.32

the failure messages you get at runtime

1207.32

are so much easier to understand and

1210.44

they're clean they're easy to read uh

1213.62

unlike a certain methods you would have

1216.32

to kind of expand upon the assert

1218.24

methods like so

1220.46

okay so now we have this one put it back

1223.28

so it passes

1224.9

so that's just basic assertion so that's

1227.539

very simple comparison between the two

1230.72

so now let's look at this chaining

1232.46

method so the second one here is

1234.919

chaining

1237.74

so

1239.299

chaining allows us to chain different

1241.7

things we can change strings

1243.26

specifically we contain collections but

1246.38

for this one let's just start with

1247.64

string

1248.6

so here we're going to assert that

1250.7

[Music]

1252.039

johnsmith.getname dot starts with John

1254.84

we can also check that it ends with th

1258.16

and we can see if it is equal to ignored

1261.26

case

1262.88

John Smith

1264.74

again if we wanted to do this as assert

1268.4

you would have to do something like this

1272.24

so here we have to essentially write

1274.4

three assert methods to do the same

1278

thing that we can do in a search J in

1279.919

one line

1280.94

so here we can assert that John Smith

1283.1

dot get name starts with joh DOT ends

1287.72

with th dot equals ignored case John

1290.419

Smith

1291.799

a lot easier to read and very easy to

1295.76

understand where this gets convoluted or

1298.4

if you have a lot of these it's going to

1301.039

be hard to tell what exactly the test is

1303.08

doing

1304.7

okay

1305.9

put those go back to clean

1311.78

run this again

1314.9

so if we look at our chaining it passed

1319.52

now let's make it fail

1322.1

so let's say we did John by mistake

1326.36

the first part should fail that's passed

1329.059

that should still pass but the first

1330.74

part failed

1334.039

foreign

1336.76

and if we look we were expecting John

1339.98

Smith to start with John and it failed

1343.34

because

1345.32

um our name was actually j-o-h-n

1348.98

so it did not start with John so it

1351.44

failed

1352.88

very clean very easy to read

1356

okay put that back together H so this

1358.82

passes

1360.919

all right so that's chaining

1363.5

hello this is Rob with developmentor

1365.9

also known as building better developers

1368

wanted to announce that we have

1370.36

school.developmentor.com feel free to

1372.679

check it out if you like any of this

1374.84

information any of the content that

1376.76

we've sent and you would like to see

1377.96

more you can come out you can enroll for

1380.059

free we have free courses we've got

1382.34

places for you to get better at just

1384.98

learning a technology our how to's you

1387.679

can work on your business skills we can

1389.72

help you with becoming a better

1391.159

developer as encoding and things like

1393.559

that a lot of the stuff you've seen on

1395.24

YouTube we also have out at

1397.4

school.develop anymore we just have it a

1399.679

little more of a educational format and

1402.2

a way for you to track your progress as

1404.72

you move forward becoming a better

1406.88

developer

1408.44

foreign

1415.22

[Music]