Before you go further, see how to test to see if an expected exception has been thrown.
- [Narrator] Hello and welcome to Chapter four. In this chapter we're going to continue to work on our ISBN validator, however, I want you to get the chance to go through the three stage process of writing the test, writing the code and then refactoring. So throughout this chapter I'll be suggesting that you pause the video and give each step a go before you press play to check that you've done something similar to what I'm going to do. So, let's make a start with this and there are a few more bits of knowledge about ISBN numbers that we need to deal with.
The first of these is that they must always be 10 digits long. Actually, that's not strictly true, they could also be 13 digits but that's a different piece of functionality. We're not going to worry about that right now, we're just going to deal with 10 digit numbers. So we'd like to add a test that will check the length of an ISBN number supplied is 10 digits. I think we'll write this test together because we'll need to use a feature of J unit that I haven't yet shown you and if you're new to J unit then it will be helpful to know about this.
So let's start as always with a failing test. So, we'll put in the annotation and I think we'll call this test 9 digit ISBN's are not allowed. Okay, so I want this to be a failing test to start with so let's put in a call to the fail method and we'll run this and make sure we get a red bar.
Good, so we've got a red bar and it's just this new test that's failed so we're ready to start. So, let's start by writing the code to call the validator. So, I think I'm just going to copy this one up here to start us off. So we'll have a new instance of our validator and then we're going to go check ISBN that this time passing a nine digit number. Just to make the point I'm going to put this as 123456789.
So that's clearly a nine digit number, that can't possibly be a valid ISBN number. Now, we need to make a design choice here. I think that if the ISBN number is wrong then our code should probably find an exception. Now, we could create our own exception but I think Java's number format exception would work fine for this. So, let's expect that if we call this line here we should expect a number format exception to be thrown.
Now, there are a couple of ways that you can to do this in J unit. You could use a tri catch block, catch a number format exception and if that isn't what's thrown then make this test fail or otherwise make it pass but a much neater way is to say that we expect this test to fail exception and we do that by adding a parameter to our apt test annotation. So we need to open brackets. The name of the parameter is expected and we set that equal to the exception type that we expect with a dot class on the end.
So in this instance, I expect the lines of code in this test to flow a number format exception. I need to put a doc class on the end. So, let's save this and make sure it's going to compile. Okay, that looks good. Actually, I just got a warning here that the local value of result is not used. I'd like to try and make sure we don't have any warnings. So let's just remove the boolean result equals. We don't care what the result is. Actually what we care about is that we're going to get this exception.
So by having the annotation like this it doesn't matter what happens in this method as long as this exception is thrown. If it isn't thrown then this test will fail. If it is thrown then this test will pass. Let's run this and see what happens. Well our test didn't pass. Actually this time we got an error rather than a failure so let's have a look down here at the failure trace and I'm just going to maybe expand this window to make sure it's readable.
Actually doing that doesn't really work. I think what we'll do instead is double click on the tab to make this full screen and then we've got a better chance of being able to read what's here. So, it says that an unexpected exception, we expected our number format exception but what we got a string index out of bounds exception. That is because, actually what happened there is our code crashed. It can't code with a nine digit ISBN and it threw a string index out of bounds exception.
Our code crashed which is why we got an error rather than a failure but either way we got a failing test and that's something we now need to go and fix.