简体   繁体   English

使用Mockito isEmpty的模拟列表总是返回false,即使大小为0

[英]Mocked List using Mockito isEmpty always returns false, even if the size is 0

I'm playing with Mockito (1.9.5) and stuck at first simple test case: 我正在和Mockito(1.9.5)一起玩,并坚持第一个简单的测试用例:

List mockedList = mock(ArrayList.class);
assertEquals(0, mockedList.size()); // Passed
assertTrue(mockedList.isEmpty()); // Failed

Can anyone explain why isEmpty() here returns false while the size() returns 0? 任何人都可以解释为什么isEmpty()在这里返回false而size()返回0?

I think this happens because mockito doesn't know the semantic meaning of isEmpty() and when it encounters a boolean method mocks it with a default value that is false . 我认为这是因为mockito不知道isEmpty()的语义含义,当遇到布尔方法时,它使用默认值false模拟它。 Same think happens with size() but the default value here is 0 . 同样的想法发生在size()但这里的默认值是0

Basically, you need to define the expected behaviour of your mocked object. 基本上,您需要定义模拟对象的预期行为。 If you don't, it will return default values. 如果不这样做,它将返回默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM