0

Hi I am trying to test a remove method of ArrayList in JUnit:

@Test
 public void testRemoveCommentValidIndex() 
{

   item.removeComment(2);
    assertEquals("Remove the comment", 2, item.getNumberOfComments());

}

so I just check that the size of the ArrayList is reduced by one, which is a bit primitive, so my tutor on the lab told me that it would be better to return the removed item, using reflection, in order to make sure that we removed the right item, which I'm not sure how to do.

Any help would be very much appreciated.

V.Savage
  • 61
  • 1
  • 2
  • 6
  • Er, did you try the javadocs? `ArrayList.remove()` returns the item removed. http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html#remove(int) – Stewart Nov 05 '15 at 11:44
  • 1
    does anything speak against invoking list.contains() on the item returned by remove? – Rhayene Nov 05 '15 at 11:48

4 Answers4

0

I think your method is reasonable, but I would additionally validate the state before:

@Test
public void testRemoveCommentValidIndex(){
    assertEquals("Before the remove", 3, item.getNumberOfComments());
    item.removeComment(2);
    assertEquals("Remove the comment", 2, item.getNumberOfComments());
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

You can do this:

@Test
 public void testRemoveCommentValidIndex() 
{

    Comment commentThatShouldBeRemoved = new Comment();
    //now set values of that comment in order to be equal to comment that 
    //will be removed

    Comment commentRemoved = item.removeComment(2);
    assertEquals("Remove the comment", 2, item.getNumberOfComments());
    assertEquals("Comment removed", commentThatSHouldBeRemoved, commentRemoved);
}
Héctor
  • 24,444
  • 35
  • 132
  • 243
  • The problem is that the item.removeComment(index) method is void, and I'm not allowed to change it, so Comment commentRemoved = item.removeComment(2); does not work it says incompatible types, void cannot be converted to Comment – V.Savage Nov 05 '15 at 15:25
  • Well, if your method return void and you can't change it, I'm afraid that there is no solution, even with reflection. – Héctor Nov 05 '15 at 15:30
  • That's the problem with some lab sessions, so that they don't allow you to change the code, however, using your code and adding some reflection I came up with this: – V.Savage Nov 05 '15 at 15:43
  • public void testRemoveCommentValidIndex() throws Exception { Comment comment = new Comment("Vandal Savage", "It's fine", 3); Field field = item.getClass().getDeclaredField("comments"); field.setAccessible(true); Comment commentToRemove = (Comment)field.get(2); item.removeComment(2); assertEquals("Comment removed", comment, commentToRemove); assertEquals("The coment removed", 2, item.getNumberOfComments()); } – V.Savage Nov 05 '15 at 15:43
  • Although I still cannot this: Comment commentRemoved = item.removeComment(2); so I am not sure if my version is completely correct. What do you think? and thank you man for your help, I really appreciate it. – V.Savage Nov 05 '15 at 15:46
0

If you're using remove(int) on the ArrayList, it returns the object that was removed from the collection. You shouldn't use reflection for something like this. You can use either the method I referenced or just iterate over the list and inspect each member. Reflection is massive overkill.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
0

You can do a better assertions with Hamcrest library. In this link: How do I assert a List contains elements with a certain property? explain how you can check if an object is contained into ArrayList.

Community
  • 1
  • 1
viticlick
  • 155
  • 1
  • 9