views:

180

answers:

5

My Junit test should check that the number being returned is the same as the list size

if my test was as follows would this be coded properly, i feel it isnt because i the last 2 lines are always going to be true?

public void testGetTotalPilots() {
    ArrayList<Pilot> list = new ArrayList<Pilot>();
    int size = list.size();

    assert size == list.size();
}
A: 

I wouldn't say that you test is correct.

public void testGetTotalPilots() {
    ArrayList<Pilot> list = new ArrayList<Pilot>();
    int size = 0;

    assert size == list.size();
}

The change above would make sure that your test is always going to be zero as that appears to the expected result. If it isn't zero then the test will fail.

AutomatedTester
+3  A: 

You could test, e.g., whether the list is empty (size == 0) when the list was created:

ArrayList<Pilot> list = new ArrayList<Pilot>();
assertEquals(0, list.size()); 
// or: assertTrue(list.isEmpty());

And/or you could add some entries to the list and check whether the list's size is set accordingly:

list.add(pilot1);
list.add(pilot2);
assertEquals(2, list.size());

Edit:

When unit testing a list (ArrayList), you should think about how the list can be used (test scenarios). For instance,

  • when a list is created, it should be empty
  • when an item is added to an empty list, its size should be one (afterwards)
  • when a list contains one item, removing this item should result in an empty list
  • and so on...
Frank Grimm
+1  A: 

Surely your test needs to do something first, i.e. trigger the code you're testing to populate the ArrayList of Pilot objects? So, you would then code your test as follows:

public void testGetTotalPilots()
{
    ArrayList<Pilot> list = new ArrayList<Pilot>();
    // Invoke the code that populates the Pilot list
    int size = 3;    // Set this to the expected number

    assertEquals(size, list.size());
}

…  otherwise you're just not testing anything.

Ben Poole
Yes, the original question just tests whether or not the Java ArrayList API is working correctly - it's probably safe to assume it is.
cyborg
A: 

What you are testing here is ArrayList. Why? I don't think it is particularly useful to spend your time testing basic functionality of classes which ship with Java and which are in wide use.

Looking at the name of the test this test should rather get the list from somewhere else and then test assumptions about the list of pilots. Something along the lines of

List<Pilot> pilots = pilotDao.getAll();
assert pilots.size() == 0;

Pilot newPilot = new Pilot();
pilotDao.addPilot(newPilot );

pilots = pilotDao.getAll();
assert pilots.size() == 1;
assert pilots.get(0).equals(newPilot);

Or, as I said in your other post, do this; List pilots = pilotDao.getAll(); pilots.add(new Pilot) and then check whether the right reaction to this happens, i.e. an exception is thrown if you don't want people to modify the list or a subsequent call to pilotDao.getAll(); returns a list of size 1.

Thomas Lötzer
+1  A: 

assert is a java language feature not to be used in tests. It is evaluated with runtime option -ea only! Without that option, your code tests nothing! Please use methods of junit.framework.Assert like assertEquals().

On the other hand, i am not sure, what your test should test. list.size() is always list.size(). You should assert a number. Also you need not to test ArrayList - it works. Where is your code to test?

Arne Burmeister
This may be true for JUnit, but there are other test frameworks. In TestNG for example, assert is the preferred way of stating assertions, see http://testng.org/doc/documentation-main.html#success-failure
Thomas Lötzer
The question starts with "My Junit test". Also TestNG has an own assertion class (org.testng.Assert), the documentation also remarks to enable assertions to make the test work.
Arne Burmeister