Create a mocked list by mockito

后端 未结 3 686
花落未央
花落未央 2021-02-01 15:19

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

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

Here is my implementation:

相关标签:
3条回答
  • 2021-02-01 15:51

    When dealing with mocking lists and iterating them, I always use something like:

    @Spy
    private List<Object> parts = new ArrayList<>();
    
    0 讨论(0)
  • 2021-02-01 16:04

    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.

    0 讨论(0)
  • 2021-02-01 16:06

    We can mock list properly for foreach loop. Please find below code snippet and explanation.

    This is my actual class method where I want to create test case by mocking list. this.nameList is a list object.

    public void setOptions(){
        // ....
        for (String str : this.nameList) {
            str = "-"+str;
        }
        // ....
    }
    

    The foreach loop internally works on iterator, so here we crated mock of iterator. Mockito framework has facility to return pair of values on particular method call by using Mockito.when().thenReturn(), i.e. on hasNext() we pass 1st true and on second call false, so that our loop will continue only two times. On next() we just return actual return value.

    @Test
    public void testSetOptions(){
        // ...
        Iterator<SampleFilter> itr = Mockito.mock(Iterator.class);
        Mockito.when(itr.hasNext()).thenReturn(true, false);
        Mockito.when(itr.next()).thenReturn(Mockito.any(String.class);  
    
        List mockNameList = Mockito.mock(List.class);
        Mockito.when(mockNameList.iterator()).thenReturn(itr);
        // ...
    }
    

    In this way we can avoid sending actual list to test by using mock of list.

    0 讨论(0)
提交回复
热议问题