mardi 27 septembre 2016

Mocked method (B) in a class calls real method(B) while calling real method(A). Method B is being called in method A

I have been trying to resolve this issue for quite a while now, I am trying to mock methodB() in a class where the real methodB() is being called. I am calling the methodA() which has a call to methodB() inside it. Also, both the methods are in the same class. I have also tried these two links, where the usage of spy() is recommended.

Note: The issue only arises when I try to mock a method in the same class. This does not happen when I try to call a method from a different class, since the mocked implementation of that kicks in. Please help.

Thank you.

Using Mockito to stub and execute methods for testing Use Mockito to mock some methods but not others

Class PolicyTest {
    @Mock    
    @Autowired 
    PolicyService policyServiceHistory;
    List<PolicyDetail> policyDetails = new ArrayList<PolicyDetail>();

@Before
public void setup() {

   policyServiceHistory = mock(PolicyServiceImpl.class);

}

@Test
    public void getPolicyHistory() throws ParseException {

    PolicyServiceImpl policyService = new PolicyServiceImpl();
//mocked the method using the mocked object    
when(policyServiceHistory.getPolicyDetails(anyString(),(Date) any(),anyString())).thenReturn(mockPolicyDetails);
//calling the real method using the real object    
List<PolicyDetail> policyDetails = policyService.getPolicyDetailsHistory(mockCompany);


}


Class PolicyServiceImpl {

          public List<PolicyDetail> getPolicyDetailsHistory() {

        //real method getting called even though its return value is  //mocked in the test method
        List<PolicyDetail> policyDetails = getPolicyDetails(name, effdate, companyName);


}

   public List<policyDetail> getPolicyDetails(String name, Date effDate, String companyName) {

}


}

Aucun commentaire:

Enregistrer un commentaire