Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a mocked list by mockito

I want to create a mocked list to test below code:

 for (String history : list) {         //code here     } 

Here is my implementation:

public static List<String> createList(List<String> mockedList) {      List<String> list = mock(List.class);     Iterator<String> iterHistory = mock(Iterator.class);      OngoingStubbing<Boolean> osBoolean = when(iterHistory.hasNext());     OngoingStubbing<String> osHistory = when(iterHistory.next());      for (String history : mockedList) {          osBoolean = osBoolean.thenReturn(true);         osHistory = osHistory.thenReturn(history);     }     osBoolean = osBoolean.thenReturn(false);      when(list.iterator()).thenReturn(iterHistory);      return list; } 

But when the test run it throw exception at line:

OngoingStubbing<DyActionHistory> osHistory = when(iterHistory.next()); 

for details:

org.mockito.exceptions.misusing.UnfinishedStubbingException:  Unfinished stubbing detected here: -> at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)  E.g. thenReturn() may be missing. Examples of correct stubbing:     when(mock.isOk()).thenReturn(true);     when(mock.isOk()).thenThrow(exception);     doThrow(exception).when(mock).someVoidMethod(); Hints:  1. missing thenReturn()  2. you are trying to stub a final method, you naughty developer! 

How can i fix it ? Thanks

like image 904
Tien Nguyen Avatar asked Aug 29 '13 14:08

Tien Nguyen


People also ask

What can be mocked with Mockito?

The Mockito. mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called. We don't need to do anything else to this method before we can use it.

How do you initialize a mocked object?

For the mocks initialization, using the runner or the MockitoAnnotations. initMocks are strictly equivalent solutions. From the javadoc of the MockitoJUnitRunner : JUnit 4.5 runner initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.


1 Answers

OK, this is a bad thing to be doing. Don't mock a list; instead, mock the individual objects inside the list. See Mockito: mocking an arraylist that will be looped in a for loop for how to do this.

Also, why are you using PowerMock? You don't seem to be doing anything that requires PowerMock.

But the real cause of your problem is that you are using when on two different objects, before you complete the stubbing. When you call when, and provide the method call that you are trying to stub, then the very next thing you do in either Mockito OR PowerMock is to specify what happens when that method is called - that is, to do the thenReturn part. Each call to when must be followed by one and only one call to thenReturn, before you do any more calls to when. You made two calls to when without calling thenReturn - that's your error.

like image 114
Dawood ibn Kareem Avatar answered Sep 29 '22 22:09

Dawood ibn Kareem