Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mockito when().thenReturn doesn't give expected result

I am new to Junit and Mockito.
Trying to mock one of the object of the class, but it is not working.
The mock method is returning an empty list, due to which test case is getting failed.
This is the code which I have written.

Junit Test Class : Here I have mocked the object and method to return an Arraylist, but when the code is executed this mock method is returning an empty list due to which test case is getting failed.

package com.business;

import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.data.api.ToDoService;

public class TodoBusinessImplMockTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testRetrieveTodosRelatedToSpringUsingMock() 
    {
        ToDoService todoServiceMock = mock(ToDoService.class);
        
        List<String> todoList=Arrays.asList("Learn Spring MVC", "Learn Spring","Learn to Dance");
        
        Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);
        
        TodoBusinessImpl todoBusinessImpl = new TodoBusinessImpl(todoServiceMock);
        
        List<String> todos = todoBusinessImpl.retrieveTodosRelatedToSpring("Ranga");
        
        assertEquals(2, todos.size());
    }

}

Interface : ToDoService.java

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package com.data.api;

import java.util.List;

public interface ToDoService {
    public List<String> retrieveTodos(String s);
} 

TodoBusinessImpl.java

package com.business;

import java.util.ArrayList;
import java.util.List;

import com.data.api.ToDoService;

public class TodoBusinessImpl {
    
    private ToDoService todoService;

    TodoBusinessImpl(ToDoService todoService) {
        this.todoService = todoService;
    }

    public List<String> retrieveTodosRelatedToSpring(String s) {
        List<String> filteredTodos = new ArrayList<String>();
        List<String> allTodos = todoService.retrieveTodos(s);
        for (String todo : allTodos) {
            if (todo.contains("Spring")) {
                filteredTodos.add(todo);
            }
        }
        return filteredTodos;
    }
}

>Solution :

Your spec says:

Mockito.when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todoList);

but your call uses:

todoBusinessImpl.retrieveTodosRelatedToSpring("Ranga");

"Ranga" isn’t "Dummy", therefore your spec isn’t matched; therefore mockito returns the default result (which would be an empty list).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading