Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito:X调用时返回NULL_Java_Mockito - Fatal编程技术网

Java Mockito:X调用时返回NULL

Java Mockito:X调用时返回NULL,java,mockito,Java,Mockito,我必须模拟BufferedReader以返回我想要的值: BufferedReader readerMock = mock(BufferedReader.class); when(readerMock.readLine()) .thenReturn("firstLine") .thenReturn("secondLine") .thenReturn(null); for (String next = reader.readLine(); next != null; next = r

我必须模拟
BufferedReader
以返回我想要的值:

BufferedReader readerMock = mock(BufferedReader.class);
when(readerMock.readLine())
  .thenReturn("firstLine")
  .thenReturn("secondLine")
  .thenReturn(null);

for (String next = reader.readLine(); next != null; next = reader.readLine())
  do something...
问题:

readerMock.readLine(); //returns "firstLine" 
readerMock.readLine(); //returns "secondLine"
readerMock.readLine(); //PROBLEM: returns "secondLine" instead of NULL
问题:如何在第三次呼叫时返回
null

对我有效(Mockito 1.9.5):

输出:

firstLine secondLine null 一线 二线 无效的 为我工作(Mockito 1.9.5):

输出:

firstLine secondLine null 一线 二线 无效的