Junit ignore method mocking

Viewed 192

I have created a Junit test case in an existing class where I want to ignore few mocks

@InjectMock
private HomeService homeService;

@Mock
private HomeUtility homeUtility;

@Test
public void testIsFilterWorkedTrue() {

    homeService.getData();
}

HomeService.java

class HomeService {

    @Autowired
    private HomeUtility homeUtility;
    
    public [RETURN_TYPE] getData() {

        // call mocked method
        output = mockedMethodcall();

        test = homeUtility.callMethod(output);
        return test;
    }
}

In the above code, for homeUtility.callMethod(output);, I don't want it to be mocked and should go inside the method. I have to use

@Mock
private HomeUtility homeUtility;

because it is getting used in other methods as well. How can I ignore it

1 Answers

You can use thenCallRealMethod for ensuring the actual method is called for that object.

@InjectMock
private HomeService homeService;

@Mock
private HomeUtility homeUtility;

@Test
public void testIsFilterWorkedTrue() {
    when( homeUtility.callMethod(anyString()).thenCallRealMethod() )
    homeService.getData();
}

You can also use spy for this. Some more reading: https://www.javabullets.com/mockito-partial-mocks-mock-and-spy/

Related