In the last lesson we learn how to use a debugger in this lesson we will use debugger to fix any issue 2
in the process. 3
We're going to learn a couple of things that people learn how to write unit tests using unit. 4
And we also look at another cool feature of Eclipse debugger to debug the issue. 5
We want to ensure that the programs we write work correctly. 6
Some developers rely only on using system not older print statements to verify program correctness but 7
doing this is really low tech and is very time consuming. 8
You would have to repeat the process every time some logic is changed. 9
Similarly you cannot just rely only on using a debugger debugger should mainly be used to identify the 10
exact cause. 11
Once we are on that issue. 12
So the best runtime solution is to use automated uniqueness. 13
The benefit is that they are automated Arnalds also repeatable illness. 14
These does ensure that any new changes do not break the system so we can say that the main benefit is 15
program correctness at all points of times. 16
With that we can feel confident about our code and sleep peacefully knowing that we are in good shape 17
. 18
I'm sure you would agree that this last benefit is really important. 19
G-Unit is a popular open source framework that allows us to write automated uniqueness for Djala programs 20
that typically use the best individual unit like methods and Gagin a dot org is the official website 21
of G-Unit. 22
It was originally written by goingback Eric Gama who are well-known authors and have written some classics 23
and software engineering feat and Jaman it is well integrated into eclipse. 24
Writing a G-Unit best on a desk is as it is referred to in what setting up method input. 25
Then executing the method with the input and finally testing the actual output with the expected value 26
. 27
So let's get started. 28
Underwrite a unit task is Villatoro lot of a debugger. 29
Some real work before writing our first unit test case. 30
Let's look at some new code that has been introduced first as a department class which is a new class 31
and it has an instance variable called name which represents the department name. 32
It comes along with a getter and a setter and it is initialized in this constructor course class husbanded 33
and couple of lessons ago. 34
A new way that will call Department has been introduced here which is of pipe Department OK and the 35
department variable is initialized in the constructor or here. 36
So that department name is a new parameter Department is initialized only if the department name is 37
not equal to null. 38
So if it happens to be null then the department variable will also be null and there is also a getter 39
for the department ready next to that class a new method has been added call and roll. 40
It allows a student to enroll in a new course. 41
So courses a parameter here. 42
And if this method is invoked on a student instance it means that student is trying to add a new course 43
and a student is allowed to are only a limited number of courses on all the courses that are added are 44
stored and this data structure and road courses which is an idealist. 45
And here is the limit. 46
So we are checking that the student is not enrolling more than this limit. 47
OK so it is course underscore and underscore Lamech which is a static variable and is initialized with 48
a rally tree. 49
On this method invokes this print method print and road courses which all it does is simply iterates 50
over the root causes and Bentz all the courses that the student has registered for. 51
So it prints the course name under department name says ghost or department Daut get name on the result 52
or get her far enrolled course this variable which is the RL list which is here which is created here 53
. 54
Don't worry about this generics related BACS since generics is not yet covered. 55
Other than that I want to just refactor just rename the student test class. 56
Let's go here to refactor rename it to student test drive. 57
Let's just do this next. 58
Finish. 59
So that's how you rename it class going to refactor under your name. 60
The reason we want to rename it is because the ESC is that we are going to write in a minute will be 61
named the student test. 62
That's because that's the regular convention. 63
So if we want to test anything in student then the task is a rewrite by convention has to be student 64
dust. 65
OK it's not a requirement but just a convention. 66
That's the reason we are renaming it to student best driver it should be obvious that much of dusting 67
happens and development environment as opposed to a production environment. 68
That's because in development and Rutman code is being actively dolar and that is called POBox. 69
Testing would help in identifying and fixing those bucks on production. 70
You would expect to be near-perfect. 71
There is no need for any best guesses. 72
Mortars are running Vasquez's. 73
We're also Edam system resources as to how to execute the code they are testing. 74
So typically real code that needs to be deployed is kept separately from Vasquez's when time comes for 75
deployment. 76
Only the real code is deployed or not the task is this. 77
So let's also follow the same practice and create what gets a subsidy. 78
Let me just minimize this. 79
Let's go here into the project and let's head new. 80
To create a news source folder let's just call it best because we are creating test cases. 81
Now let's use the same package name here because we are going robustus this class student writing a 82
test case for that and it need not be the same package name but convention is to use the same package 83
name as the class being tested. 84
So let's just go ahead sip package. 85
It's the same one. 86
Now let's just go ahead and create our unit desk. 87
So go into new year after I quicken her G-Unit desk guess it's just call it call it a student test because 88
we're creating a test case for a student and that needs to be the name on the package. 89
Here is a package name on class under test would be the Student class no hit next. 90
And it was all the methods in the Student class and we want to create a test case for this method. 91
Also shows the math that's an object class because Object class is the superclass. 92
So this is the only method that we want to test had finished. 93
It says are G-Unit for a library to go Billy-Bob because it's not yet added but let's do it later in 94
a bit just from the best guess that gets created. 95
We'll see how that can be done. 96
OK. 97
So this is the test case that God created automatically and as expected it shows completion errors because 98
their uniform is not in the classpath. 99
So we need to add that library. 100
So let's just call it what it says gives three quick fixes. 101
It says you can add this unit for library book I believe but it's true that I know you can see the compression 102
errors Mandvi and it got added here so that our libraries in our other JRD under G-Unit for guess if 103
you ever want to remote just right click go to belly button Heydari Movember. 104
OK now here are the matter that needs to start is and roar and you can see that G-Unit has created this 105
desk is called best Android so it always uses this kind of a naming convention best followed by the 106
Mathare the method name that GHI that's going to be tested on. 107
Here is an on additions and additions. 108
We did not cover unpropitious yet but it's simply a moderator data and it does G-Unit that this is this 109
particular method is a best guess. 110
So if you read this and you were audited SR as an addition as G-Unit that the method to which it is 111
attached can be run as a test case to run them at their G-Unit first constructs a fresh instance of 112
the class which is the student class this class then invokes the unaudited matter. 113
Any exceptions thrown by that test will be reported as a failure if no exceptions are thrown that Destin's 114
is used to have succeeded. 115
Maybe look at both failure and succeeded. 116
And it should be clear and it should be clear to you. 117
Now here there is this matter called fail which is a static method in a class called assert which is 118
being imported here. 119
OK. 120
We haven't had Biscoe static imports. 121
If possible I might include a separate lesson for that but I think this discussion should suffice too 122
. 123
So let's start taking borders as it imports all the static members like the static field from the static 124
methods in a particular class. 125
So I don't think that this is a package. 126
It's a class actually and we are importing all the static members from it. 127
And the advantage of that is you don't have to qualified the static member using the class name you 128
don't have to do a search dogface. 129
You can invoke it directly just like invoking any method in the same class. 130
OK. 131
So that that's the I'd want it you don't have to qualify it using the class me but you must use it sparingly 132
. 133
That's because it can confuse your readers if you use it excessively. 134
It might confuse the readers of your code or you yourself because it's not clear whether you're invoking 135
a method in this class or if you are working a method within this class. 136
OK so use it sparingly and just go ahead and run this. 137
And it says of one failure. 138
The reason is because this method is responsible for that. 139
What this method means is it just means that the best guess is incomplete and new to selling the dodo 140
bird that you need to implement it sooner it's just be some real code here. 141
I do a control issue f on what exactly are we testing here. 142
We are testing to ensure that student is not allowed to enroll in more than that the limit the max limit 143
number of courses. 144
OK. 145
So should a student should enroll only in three courses. 146
So here is how we set up that task is sort of the input. 147
So we're creating a student instance here first then we are creating a course instance instance and 148
passing for other department and we are doing the method execution. 149
So we are in walking the enrollment third installment by passing discourse one variable which is a course 150
which is of course. 151
And finally we are testing the output though the enrollment there is not retaining anything. 152
We are testing. 153
There is a change of speed when when this method is getting executed and we are checking that. 154
So what are we dusting here. 155
We're getting all the courses that the student has enrolled in. 156
So we are sort of credence on our list with all the courses that the student has enrolled and we are 157
calling the size method of idlest undertows how many courses the student has registered and it should 158
be less than the limit that we have. 159
OK. 160
And guess if if it is less we are good about but if it exceeds then it means something wrong has happened 161
. 162
Logic has been written in a wrong way and G-Unit would throw an error on it or spread this message. 163
Enrollment limit exceeded should never happen. 164
So and then we are adding some more courses here. 165
Nothing more than that. 166
So as Drew is also a method in the class. 167
So let's just go ahead and run this. 168
You know it says one error failure zero. 169
So what is an error what is a failure or is basically some kind of an internal error like a null pointer 170
exception or something like that. 171
And if failure is something like that's where you're testing some expected value with the actual or 172
they are not meeting that is expected is different from the actual and then Junod were throw we would 173
say that a failure has happened. 174
So what is the error here. 175
It says null pointer exception which means that something went wrong. 176
This is the stack trace and it's just DoubleClick and debug it using over debugger. 177
So let's go here. 178
This is a matter where some issue has come up. 179
So let's just set up our break point here and let's call our friend debugger tool and test Java. 180
And here it stops. 181
So the issue is null pointer exception so we don't know whether it is cause is null courses here or 182
with of course don't get Department we are displaying the department name. 183
So we are saying get Department we don't know if this is not. 184
So one of them is null. 185
But in this case it's obviously you know if you holler over course we know that it is not. 186
No but it could also be know what caused our get Department is obviously not here. 187
So but let's just pretend that we don't know and let's try to figure that out. 188
So for that what you can do is we can use a new feature of Eclipse guard expeditions where we were test 189
expression expression can be coarse expression can be course not good department. 190
So let's just see if any of these expressions is not sort of just going to window which says sure you 191
hit exhibition's here. 192
Yes and it's an expression. 193
It's just called this course which is an expression here and it has some value which means it does not 194
know. 195
So that's not that's not an issue with that. 196
Another way to add exhibition is just copy this on our new exhibition and listed there. 197
Or you can also do this you can head control shift. 198
I mean you do that it opens this pop up and it shows the value. 199
Now if you want this to show up in that exhibitions you hit Control shift I one more time. 200
And there you go. 201
Here the value. 202
So that's the null value. 203
So when we are in we'll can get name on this Nuddle it is giving us a null pointer exception. 204
So we need to fix that and for that we need this kind of a statement. 205
So you would execute this system not out statement only if Gore's Target department does not equate 206
to none. 207
So that's the fix that we need to have. 208
OK. 209
But what happened when I just we just added this court thought stop in the same debug session we did 210
not stop the debug session. 211
So we have just added on the fly we have added our fix on the fly. 212
And when we saved it controlled went back to the first day first line of the matter. 213
OK. 214
Now we can test it right. 215
So let's just hit her as you just hit her is the industry on it's just hit or is you 216
going to G-Unit. 217
We actually have some other failure but this one is gone. 218
This area is not that it was something else we will figure that out. 219
But the main thing as exhibitions are very useful when you have a chain of in locations like caused 220
or get Department thought let's say something else get something DOD get something. 221
And that is you would you know if there is a null pointer exception you don't know which is which subexpression 222
is good but is not. 223
So you would you know an inexperienced Dolapo would keep printing each of the invocations here using 224
a system not out and we're trying to figure it out. 225
But the thing you can do is you can always do this. 226
UNDERSHAFT I know whether it's down whether it's in Larnaca. 227
Now if it does not matter then you would expand it and you will try for this. 228
So there are a chain of indications chain off in locations like if you a call this has a or b b c c 229
or d. 230
So if you have a chain of invocations then exhibition's who would be perfect. 231
OK now let's just go up go back to our unit. 232
Let's see what the issue is. 233
It says best Andrle. 234
It's a failure which means that the expected value did not meet the actual value. 235
So let's just click on that and here it is. 236
And the last line there is some issue. 237
The issue is let's just going to unroll matter here. 238
Let's not use our debugger. 239
So here the issue is it says less than or equal to cause and No-Limit. 240
This should have been less than similar. 241
Because of that it was showing an error even when the fault course was added. 242
So if you go back to student dust. 243
So I suppose the first course that got added and the logic shouldn't have allowed us to added and allowed 244
us to add on so when we try to test it it failed and it helped us to fix this logic issue. 245
So that's about it. 246
That's all I wanted to show. 247
So we looked at how to create a test case and we also looked at the new expeditions feature of debugger 248
which is very useful when you have a chain of implications and we need to see some value more there 249
. 250
So that's about it.