ok so let's work on the last three
challenges this is challenge number
8
so the challenge here is to write another
test for the converted method that tests
the following scenario now by the way
you can add the test to the existing test
method or create a new one now the input
here is a equals 10 b equals 0 the
output arithmetic exception / 0 so
that's the test see how you go with that
pause the video and I'll see you when
you get back
ok so part of this challenge was a trick
question i'm sorry to say that's because
we can't really put the test in the same
test method because in this case we're
expecting an exception to be thrown now if
we were to put both tests in the same
method it's possible that the test
method as a whole would pass even though
one of its or one of the asserts was
failing so consequently we going to use a
new test method and will modify the org
.junit test annotation to let junit
know that we're expecting an exception
to be thrown
let's go ahead and do that i'm just
going to add this code below the first
converter test method and.....
...
alright so we'll run that and see what
happens and you can see that successfully
passes and just to confirm that
obviously will pass 0 which is causing
the arithmetic exception change that to
1 should run that again this should
now fail because the exception isn't
being thrown you can see we are getting
expected exception to be thrown and we
didn't receive once so put that back to zero
it's working fine
alright next challenge challenge number
nine
now this challenge we want to reduce
some repetition all of our test methods
are now implement and our tests are
passing would want to write more test
cases to cover scenarios that the
existing ones don't test but I will
pretend that we're actually done and we
have covered all scenarios now we could stop
here but every one of the test cases
starts by creating an instance of
utilities class so the challenges is to
modify the utilities test class so it
doesn't have this repetitive code so
that's challenge pause the video and
I'll see you when you get back
ok so for the solution what we're going
to do is we're going to add a method to
utilities test that's annotated with the
at before annotation and that method
will create an instance of the utilities
class and will also have to add
utilities instance field to the class as
well so that the test methods can access
the utilities instance now since the app
before method will run before every test
case every test method will start with a
fresh instance of the utilities class
let's go ahead and do that so I'm going
to start by adding this field....
...
...and we have to do now is change the code now that
is creating a new instance just by
deleting that line i call that util
and call it util here so deleting that line i call that util
just change the references called
to utils in the method so that's
every nth character method or test
method to fixed same for the remove pairs
converter...
...
....so we've now
got no code that actually creates that
utilities class instance other than the
code on line 15 and eventually removed
all that repetitive code and removing
repetitive code is always a good thing
when your developer let's now run
the entire test suite because I think
we've done that yet so make sure that
it's all working
actually what we need to do here is just
come up here and see that we haven't
actually got an individual tests for the
entire suite now so i actually click on
that right click and run utilities tests
to run the entire suite you can see that
all five tests are now running
successfully of course now with done that
we've got a configuration to be able to
run all our tests everything is working
fine
alright next challenge challenge number
10
so we want to actually test the following
five scenarios is going to be seeing on
the next slide with the removed pairs
method so we're going to test the
following inputs and outputs now you
could add all these tests to the
existing test method or write a method
for each set of input output but that
probably wouldn't be the best way to do
it
I want to do it in such a way that you
only have to write one test method
that's as a hint you have to create a new
class that's using a particular j unit
annotation you can see the inputs and
the outputs on the screen so taken out
of those you'll be testing those to make
sure that your matching the input
and output and that all your tests work
alright so see how you go with that pause the
video and I'll see you when you get back
alright so the solution really called
here for a parameterised test
so what we're going to do is create a
new class called utilities test
parameterised and annotate it with the
at runs with annotation let's go ahead
and do that new Java class utilities
test parameterised going to annotate it
with the @....
...
...now declared some
variables for the utilities instance the
input value for each test as well as the
expected value for each test and we also
declare a constructor that will accept
the accepted expected value the output
in other words and the string parameter
the input remember that for each set
of test data junit will create a new
instance of the class and pass the
expected and actual values to the
constructor
ok so we want...
...
...so we now add the
method that contains our expected and
actual values we have to annotate this
method with the at parameterised
. parameters annotation and also if you
recall it must return the collection
so its going to be public static...
...
....
...
....
...so that's our collection now our
test conditions and now we need to also
add a setup method that's annotated with
that before and also a test method to
actually do the testing
let's go ahead and do that...
...
and then as i mentioned on the bottom we
want to test so it's going to be....
...
let's just run this to confirm that it
does work and what I forgot to do was
add the annotation for the test
conditions line so let's go ahead and do
that
...
so now if we go back here actually run
this
so you can see there now we've got
these 40 1 2 & 3 so we actually have
four tests and they all past separately
and then we're good to go
and you also saw that created a new
run configuration for that method if we
wanted to we can just right-click even
though we've just got the one test and
run the utilities test parameterised and
that would create the run configuration
setting the only future tests will also run and we
would just have to click on the one in
the gutter to run the individual one
so that's it you've now completed the j
unit challenges and that's the end of
this section
hopefully they've got a much more solid
understanding of the basics of j unit
you can start applying that to into your
java projects