Abhishek Thakur
Abhishek Thakur

Reputation: 23

Code coverage for service class method

I'm trying to do the code coverage for a method of service class with mockito. I'm new with mockito and tried to mock the call to service method but the code coverage is 0. I'm not sure if I can mock call or can I for this particular method. If you have any suggestions please let me know. Code:

public List<Something> getTrackerData(String startDay,List<Something> someList,  boolean check,String name)
{
//filled with many if else checks
}

This is the method i want to do coverage for.

@InjectMocks private TrackerService trackerService;

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
}

@Test
    public void test01()
    {
        Something ttd =new Something();
        ttd.setCoverageSubType("None");
        ttd.setCoveredAmount("1.0");
        List<Something> list = new ArrayList<>();
        list.add(ttd);

        List<Something > newlist = mock(List.class);
//      when(trackerService.getTrackerData("", newlist, false, "")).
//      thenReturn(list);

        resultList = trackerService.getTrackerData(anyString(), anyList(), anyBoolean(), anyString());

    }

This is the test i have written. I realize that when and Mockito.verify() works only on mocked dependencies. But here I don't have any dependency to mock. So, the question is can I mock the call to trackerService.getTrackerData() with Mockito stub parameters or I have to test the method with actual parameters only ?

Upvotes: 1

Views: 2134

Answers (1)

ashoksl
ashoksl

Reputation: 383

TrackerService is already using InjectMocks. So you simply can use Mockito.when().

List<Something> result = new ArrayList();
//populate list
Mockito.when(trackerService.getTrackerData(anyString(), anyList(), anyBoolean(), anyString())).thenReturn(result);

Upvotes: 1

Related Questions