expected index out of bound exception
my teacher give me this code:
@Test(expected = IndexOutOfBoundsException.class)
publicvoid testShouldThrow(){
OurList<String> list =new OurArrayList<String>();
list.insertElementAt(1,"A");
}
I don't understand what he means by the expected = IndexOutOfBoundException.class
I mean what he is trying to say here is that I can't insert an element at index 1 if index 0 is not yet filled. But I tried this code on j unit and it compiles? Can somebody explain this to me
It compiles, but at runtime it is expected that it will throw the exception in question, which it will because you can't insert an item into an empty collection, at index 1.
I know it compiles but for I tried this J unit and it shows the green bar, isn't it suppose to show the red bar because an element is inserted at index 1?
> I know it compiles but for I tried this J unit and it
> shows the green bar, isn't it suppose to show the red
> bar because an element is inserted at index 1?
No, the test passed because it expected the exception, which did happen. The exception was thrown, and the JUnit framework caught it and determined it was of the expected type.
I see..so the test passed because the indexOutOfBound worked. Can you tell me what does the syntax means?expected IndexOutOfBoundException.class
> I see..so the test passed because the indexOutOfBound
> worked. Can you tell me what does the syntax means?
>
> expected IndexOutOfBoundException.class
Well I'm not so familiar with the annotation syntax and wasn't aware of JUnit having @Test(...) annotations, but I'd suggest reading up on JUnit at their website http://junit.org/index.htm
But intuitively speaking, it means when the method terminates, the JUnit framework sees the return value or exception, and compares its class with the above. Basically they have a catch block behind the scenes something like this:
...
catch (Throwable t)
{
if (t.getClass() == expectedClass)
testPassed();
else
testFailed();
}
where expectedClass in this case equals IndexOutOfBoundsException.class
I did the same thing as my teacher with my code, but somehow it gives me the red bar when running the j unit test.. here is my code:
@Test(expected = IndexOutOfBoundsException.class)
public void testInsertElementAtandGetElementAt() {
Movie m1 = new Movie("The Matrix Revolutions", 4);
Movie m2 = new Movie("The Lord of the Rings, Return of the King", 5);
Movie m3 = new Movie("Click", 2);
PriorityList list = new ArrayPriorityList();
list.insertElementAt(-5, m1);
> PriorityList list = new ArrayPriorityList();These are not standard classes / interfaces, so I couldn't speak as to what will happen when you invoke insertElementAt(-5, ...) on that.But the JUnit output tells you what happened. Why don't you look at that?
it gives me an index out of bound error which it shouldn't be because I already put expected IndexOutOfBoundException.class. The PriorityList is just a class that I use using the array as a data structure. In this case the index cant be less than 0.
> it gives me an index out of bound error which it
> shouldn't be because I already put expected
> IndexOutOfBoundException.class. The PriorityList is
> just a class that I use using the array as a data
> structure. In this case the index cant be less than 0.
Then I'd have to guess (since you didn't show the actual error message obviously) that the exception thrown isn't really java.lang.IndexOutOfBoundException, or that you have a home-made IndexOutOfBoundException class in your classpath, so the (expect = IndexOutOfBoundException.class) refers to a different class, not the built-in java.lang.IndexOutOfBoundException class.
that is true that I have made another indexoutofboundexception:
which is in the insertElementAtMethod()
public void insertElementAt(int index, E el)throws IndexOutOfBoundsException {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException();
for (int i = size(); i > index; i--)
data[i] = data[i - 1];
data[index] = el;
if (index == size)
size++;
}
my professor also made an IndexOutOfBound in the method but it works
No, I'm not talking about you throwing an exception. I'm talking about the possibility that you have a different class.Sorry I have to give up now though. This is not really going anywhere. I think you need someone hands-on to tutor you one-on-one.
I do have a different class. The test method are in the testClass and there is also the ArrayPriorityList class