-3
Class Elem{
    private ArrayList<someType> arr = new ArrayList<>();

   public void addElement(someType var) {
       arr.add(var);
   }

    public someType bestelement() {
        someType first= arr.get(0);
        arr.remove(0);
        return first;
    }
}

I have written test case for this method and it's running successfully but I need to know how can I be sure that the remove() method was called and the size of array list was reduced by 1? Please do tell me how to get the size of arr in my test case?

Test case for this method

Class ElemTest{
    private Elem obj = new Elem();
    private someType var1;
    private someType var2;
    private ArrayList<someType> testArr = new ArrayList<>();

    @Test
    public void bestElementTest() {
        obj.addElement(var1);
        obj.addElement(var2);
        testArr.add(var1);
        testArr.add(var2);
        someType result = testArr.get(0);
        assertEquals("test failed", result, obj.bestElem());
    }
}
  • We have too few information to answer. The posted code doesn't compile, and if it did, it would always throw an exception. – JB Nizet Feb 24 '15 at 07:32
  • This code doesn't even compile - you can't have another statement after `return`. – Mureinik Feb 24 '15 at 07:32
  • 1
    @Mureinik Have a look now. – Rahul Bharti Feb 24 '15 at 07:42
  • 1
    It still doesn't compile. And it would still always throw an exception if it did. Show us real, compiling code. How is the list populated? WHat are the other methods of the class? These are critical information. – JB Nizet Feb 24 '15 at 07:45
  • @JBNizet.. The code is huge. There is a method which adds element to the array.Everything is working fine. I just want to know is there any way I can get to know that size of array list was reduced by 1. I just want to make sure remove() method was called. Is there any way to do that? – Rahul Bharti Feb 24 '15 at 07:48
  • The best we can tell is: call bestelement() again, and check it throws an exception. – JB Nizet Feb 24 '15 at 07:54
  • Who cares if the internal array is reduced by one? You should write a test that defines the behaviour .. given a collection of elements, specify what defines 'bestelement' and write a test to assert that the correct one is returned. Then another test that when best element is called again it returns the next best element etc. until it throws an exception – blank Feb 24 '15 at 08:59
  • @blank...Thanks a lot..that worked.. :) – Rahul Bharti Feb 24 '15 at 09:06

1 Answers1

1

Given your example its difficult to advise you, but in general terms you options are to set the class into a known state, call you method which chnages the class and then check that the state has changed in the way you expect.

In this specific case if you have access to the internal list somewhere then you can check that the list has changed (ie the element has been removed). If you don't have access to the internal list then you need to check other things, like:

  • if you call bestElement() again when the object contains more than 1 element you get a different element
  • if you call bestElement() again when the object should not have any more elements you get an exception

These obviously rely on being able to set the object into one of the above states and we don't know if that is possible from the sample.

My final piece of advise would be to focus the test on the behaviour that you want to see from your class, not in the implementation of how the class works.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181