java JUnit 返回 int 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1939080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JUnit Return int value
提问by tommy
My JUnit test should check whether the number being returned is the same as the list size.
我的 JUnit 测试应该检查返回的数字是否与列表大小相同。
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 testGetTotalPilots() {
final ArrayList<Pilot> list = new ArrayList<Pilot>();
final int size = list.size();
assert size == list.size();
}
回答by Arne Burmeister
assertis a java language feature not to be used in tests. It is evaluated with runtime option -eaonly! Without that option, your code tests nothing! Please use methods of junit.framework.Assertlike assertEquals().
assert是一个不能在测试中使用的 Java 语言特性。它仅使用运行时选项进行评估-ea!如果没有该选项,您的代码将不会测试任何内容!请使用junit.framework.Assertlike 的方法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?
另一方面,我不确定您的测试应该测试什么。list.size() 总是 list.size()。你应该断言一个数字。你也不需要测试 ArrayList - 它可以工作。你的代码在哪里测试?
回答by Frank Grimm
You could test, e.g., whether the list is empty (size == 0), when the list was created:
例如,您可以size == 0在创建列表时测试列表是否为空 ( ):
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,
在对列表 ( ArrayList) 进行单元测试时,您应该考虑如何使用列表(测试场景)。例如,
- 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…
- 创建列表时,它应该是空的。
- 当一个项目被添加到一个空列表时,它的大小应该是一(之后)。
- 当列表包含一项时,删除该项应该会导致一个空列表。
- 等等…
回答by Ben
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:
您的测试当然需要先做一些事情,即触发您正在测试的代码以填充 Pilot 对象的 ArrayList 吗?因此,您可以按如下方式对测试进行编码:
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.
……?否则你只是没有测试任何东西。
回答by Thomas L?tzer
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.
您在这里测试的是 ArrayList。为什么?我认为花时间测试 Java 附带的和广泛使用的类的基本功能并不是特别有用。
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.
或者,正如我在你的另一篇文章中所说,这样做;列出飞行员 =pilotDao.getAll(); pilots.add(new Pilot) 然后检查是否发生了正确的反应,即如果您不希望人们修改列表或随后调用pilotDao.getAll();返回大小为 1 的列表,则会引发异常。
回答by AutomatedTester
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.
上面的更改将确保您的测试始终为零,因为这似乎是预期的结果。如果它不为零,则测试将失败。

