开发者

JUnit Return int value

开发者 https://www.devze.com 2022-12-14 17:30 出处:网络
My JUnit test should check whether the number being returned is the same as the list size. If my test was as follows, would this be coded properly? I feel that it isn\'t, as the last two lines are al

My JUnit test should check whether the number being returned is the same as the list size.

If my test was as follows, would this be coded properly? I feel that it isn't, as the last two lines are always going to be true? (Sorry, if this makes no sense!)

public void testGe开发者_运维问答tTotalPilots() {
    final ArrayList<Pilot> list = new ArrayList<Pilot>();
    final int size = list.size();

    assert size == list.size();
}


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?


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

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

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…


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.


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.


I wouldn't say that your test is correct.

public void testGetTotalPilots() {
    final ArrayList<Pilot> list = new ArrayList<Pilot>();
    final 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 be the expected result. If it isn't zero, the test will fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消