 Hi guys. So today we're going to be taking a look at how to trace an error when a JUnitTest case fails. So for our first example, we have this countOdd method. And what this does is it just takes an integer array and it should return the number of odd numbers in the array. It should also return 0 if the array is null or empty. So we have our method here and then we have our JUnitTest underneath. So let's just go ahead and run this and see what happens. So we can see here that when we run the program, we're encountering an error. And if we double click here, we can see the error is occurring in the first test. So we can now look down in the failure trace. And the failure trace is really helpful for telling you what kind of error is occurring or in what way your test is failing. So we can see here at the top that we're encountering a null pointer exception. And that's really useful information, but how then do we figure out where in the method the program is failing? What we can do is run the program through the debugger. So what I'm going to do is I'm going to add breakpoints throughout my method. And it's really useful, especially if you're really unsure about where your bugs are occurring, where your program is failing to be really liberal in adding breakpoints throughout your method. That way it's really easy to step through your program step by step in the debugger and figure out what's going on. So let's now go ahead and run the debugger and I'll just move to the debug perspective here. Alright, so in our variables, we can see we have our array data and its value is null because for the top test here, the test that we encountered the error on, we're just passing a null value into our count odd method. So let's now step through. So counter, we now have the variable counter that equals zero. And now in this for loop, we are expecting to iterate through our data array. So when we click next, you can see that the program terminates with that null pointer exception. So in this case, it's pretty easy to see what's going on. We've attempted to iterate through an array that doesn't exist because it's just null. So what we need to do is in our method, we need to incorporate some code to deal with null values. Now up here, we can see that we should be returning zero if an array is null or empty. So I'm going to include in my code also code for dealing with an empty array. So let's go ahead and write if data equals null or if data dot length equals zero. In other words, if the array data is empty, return zero. And now if I go ahead and run the program again, we can see that we're no longer getting an error, but we are getting a failure somewhere. So if we double click here, we can see the program is failing for this test right here. So again, to find out what's going on, we can first take a look in the failure trace. And we can see at the top here, we're getting an assertion failed error expected for but was zero. So what this means is that when we wrote this test, we expected the value four to be returned when we pass this array into our count odd function. But instead in our version of the function, the value zero is being returned. So clearly something's going wrong. So once again, we can run the program through the debugger to find out where exactly our failure or our bug is occurring. So first of all, what we need to do is we need to move the failing test to the top right here. And that way when we run the debugger, this failing test will be the first test that is tested. So let's again go ahead and run the debugger. So we see here we have in our variables, our data array, I'll just expand that. And that contains four values. So let's now go ahead and step through. So we have counter equals zero. We're now going to iterate through the array. And then in this if statement here, we're going to test to see if a value is odd. And if a value is odd, then we should increase the counter by one. So let's see what happens when we encounter our first odd value five. So then go here and then we can see the counter did not increase even though five is indeed an odd value. So obviously the problem with our program is occurring within this if statement here. So you can see we have if data i modulo two equals one, then the counter should be increased. But let's take data zero for example, negative five modulo two does not equal one. It actually equals negative one. So we can see here the problem is that our method is not accounting for negative values. So what we need to do is change that. So rather than this what we can do is say if data i modulo two does not equal zero, then we should increase our counter by one. And this will now account for both negative and positive odd values. So if we now go out of debug mode and run our program, we can see yay that the program is now passing all tests. All right. So moving on to our second example. Here we have a method all positive. And what this method does is it takes an integer array and it should return true if all items in the array are positive and false otherwise. So let's go ahead and run this. And when we do so, we see we encounter a failure at one of the tests. And if we double click here, we can see that this test is failing. Once again, we'll go ahead and take a look at the failure trace. And we can see that we have an assertion failed error expected false, but was true. So we expected the method to return false when pass these values, but instead it returned true. So like exactly what we did with the previous example, we're going to go ahead and run the program through the debugger. So just go ahead and add my break points liberally throughout. And then we'll go ahead and move the failed method to the top just like we did before. So you can see there really is a pattern to what we're doing. And then I'll go ahead and run the debugger to the debug method of debug perspective. All right, so we have in our variables, we have our array data. I'll expand this. Now it contains three values 10, 20 and zero. Zero is not positive, obviously. So we are expecting this method to return false. So let's go ahead and step through. So so far so good. Now we're going to with this for method iterate through our array data. So let's step through. So starting at I equals one. So data I equals 20. And we're now going to execute this if statement if data I in this case data one is greater than zero return true 20 is obviously greater than zero. So when we step through here, our program is going to return true and terminate. And this is obviously not what we wanted. So we can see the issue here is that rather than testing if a data is greater than zero, we should be testing if a value is less than or equal to zero. In other words, not positive. And if we find a value that's not positive, then we should immediately return false because obviously our array is not all positive. If we get to the end of our array and every item has been positive. So in other words, we haven't returned false. Only then should we return true. So if I go ahead and run the program now, then we should see. So we're still getting a failure, but it's no longer this test that's failing. It's this test down here. So following the exact same method we've been following. We'll take a look down in the failure trace. We see we get an assertion failed error expected false, but was true once again. So let's just go ahead and move the failed method to the top. Go ahead and make sure we've got all our break points in our program and then go ahead and run the debugger one more time. All right. So in this case, if we expand our data array, we can see that the first value in our array is negative five, which is clearly not positive. So our once again, our method should be returning false. But let's see what happens when we step through. So so far so good. Now when we get to our point where we're iterating through our array, we can see that I starts at one. So in I equals one. So the first point in our data array that we're going to be looking at is data one, which equals 10. So the program is completely ignoring the first index zero, which contains negative five. So now when we step through our program, the iterator is going to continue to increase. We're not going to encounter any negative values until finally the program terminates and returns true. Now this is obviously not what we want. So what we need to do is change our iterator so that I starts at zero. And now when we run the program, we should find that everything passes all tests. All right guys, so that's how we trace an error when a J unit test case fails. So I hope you guys found this helpful and thank you for listening.