3

Following are my classes

I have a interface with the following method.

The interface is as follows and has one of the method

public interface SimpleDocumentManager{

    public List getUserDocIdOfRelatedDocumentsForTemplate(String docType, List<String> templateNames,ZoneCriteria mainZoneCriteria,List<ZoneCriteria> detailZoneCriteria);
    }

Note : ZoneCrieria is a POJO class

I have Abstract Super class and have init method in it, i also have a Sub class which extends the Super class and overrides the init method, and in the Sub class init method i call the respective Service method. The code for the following classes are as follows.

public abstract class Superclass{
    public void init( ){

    }
}

public class Subclass extends Superclass 
{       
     public void init(){

          List<Map<String, Object>> ordersList =  getSimpleDocumentManager().getUserDocIdOfRelatedDocumentsForTemplate(OrderConstants.TYPE_NAME, Arrays.asList(getOriginalTemplateName()), getCriteriaMap(primaryRow), Collections.emptyList());
     }      
}

In the above method to get the ZoneCriteria i have written a private method in the Sub Class

private ZoneCriteria  getCriteriaMap(){
    Some logic that returns the ZoneCriteria.
}

The above method is used as one of the parameter which returns ZoneCriteria when i call the method present in SimpleDocumentManager.

And here is my test case for the Sub Class

@RunWith(PowerMockRunner.class)
@PrepareForTest({ServiceLocatorBeanFactory.class})

public class TestClass {

    @Before
    public void initialize(){
        PowerMockito.mockStatic(ServiceLocatorBeanFactory.class);
        PowerMockito.mockStatic(BusinessRulesUtil.class);
    }


    @Test
    public void testSubclass(){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ID, "POR0000001");
        List<Map<String, Object>> asList = Arrays.asList(map);

        SimpleDocumentManager simpleDocumentManager = Mockito.mock(SimpleDocumentManager.class);
        PowerMockito.when(ServiceLocatorBeanFactory.getService(SimpleDocumentManager.class)).thenReturn(simpleDocumentManager);

        PowerMockito.when(simpleDocumentManager.getUserDocIdOfRelatedDocumentsForTemplate(Mockito.anyString(),Mockito.anyList(),Mockito.any(ZoneCriteria.class),Mockito.anyList())).thenReturn(asList);

        Mockito.when(simpleDocumentManager.getUserDocIdOfRelatedDocumentsForTemplate(Mockito.anyString(),Mockito.anyList(),Mockito.any(ZoneCriteria.class),Mockito.anyList())).thenReturn(asList);

        Mockito.doReturn(asList).when(simpleDocumentManager).getUserDocIdOfRelatedDocumentsForTemplate(Mockito.anyString(),Mockito.anyList(),Mockito.any(ZoneCriteria.class),Mockito.anyList());


        Subclass subclass = new Subclass();
        subclass.init();
        subclass = Mockito.spy(subclass);
        subclass .init();

    }

I have used three ways to return value when the method in SimpleDocumentManager is called. The following are the three ways

1.PowerMockito.when(simpleDocumentManager.getUserDocIdOfRelatedDocumentsForTemplate(Mockito.anyString(),Mockito.anyList(),Mockito.any(ZoneCriteria.class),Mockito.anyList())).thenReturn(asList);

2.Mockito.when(simpleDocumentManager.getUserDocIdOfRelatedDocumentsForTemplate(Mockito.anyString(),Mockito.anyList(),Mockito.any(ZoneCriteria.class),Mockito.anyList())).thenReturn(asList);

3.Mockito.doReturn(asList).when(simpleDocumentManager).getUserDocIdOfRelatedDocumentsForTemplate(Mockito.anyString(),Mockito.anyList(),Mockito.any(ZoneCriteria.class),Mockito.anyList());

But none of the call returns me the value. When i debug the code getSimpleDocumentManager() returns the mocked object, but the method is not called.

Also i have created Subclass object in 2 ways. It is as follows

1. Directly instantiated the Subclass in test method as follows

    Subclass subclass = new Subclass();
    subclass.init();

2.Did a spy of the instantiated Subclass

    Subclass subclass = new Subclass();
    subclass = Mockito.spy(subclass );
    subclass .init();

When the method getSimpleDocumentManager().getUserDocIdOfRelatedDocumentsForTemplate(OrderConstants.TYPE_NAME, Arrays.asList(getOriginalTemplateName()), getCriteriaMap(primaryRow), Collections.emptyList())

is called i want to return "List<Map<String, Object>>" so for that i am doing this

Map<String, Object> map = new HashMap<String, Object>();
map.put("USER_DOC_ID", "POR0000001");
List<Map<String, Object>> asList = Arrays.asList(map);

and i am returning asList in the thenReturn() as follows thenReturn(asList);

while preparing the ZoneCriteria i check for a value and if that value is not present i have UserDefined Exception which is thrown.

So when i execute my test case i get that validation exception instead it should return the list that i have prepared.

I have gone through different links in stack overflow and could not get my answer. Following are the links. Link1

Link2

Link3

Please help

Community
  • 1
  • 1
Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44
  • Also i have mocked the map and list as follows Map map = Mockito.mock(HashMap.class); map.put(RuntimeDocumentConstants.USER_DOC_ID, "POR0000001"); List> asList = Mockito.mock(ArrayList.class); – Avinash Reddy Jan 27 '16 at 14:40
  • There is a whole lot going on here, but one immediate problem I see is that it doesn't appear you are setting up the static method call `ServiceLocatorBeanFactory.getService()` to be mocked correctly. Refer to the [Mocking Static Method documentation](https://github.com/jayway/powermock/wiki/MockitoUsage#mocking-static-method). – ach Jan 27 '16 at 14:50
  • @ach - I already have that code in my test class, mocking the ServiceLocatorBeanFactory i missed out adding that code here. Now i have added the same in the test class. Even though i am mocking the ServiceLocatorBeanFacotry using PowerMockito which is static class, i am still facing this issue – Avinash Reddy Jan 28 '16 at 06:23
  • @AvinashReddy could you post the assert statement you are using? – Lencalot Jan 29 '16 at 00:37
  • @AvinashReddy I think I see the problem, Can you post the code and or class that contains the getSimpleDocumentManager() method. – Lencalot Jan 29 '16 at 00:39
  • 1
    @Lencalot I think i figured out the problem. The method getSimpleDocumentManager().getUserDocIdOfRelatedDocumentsForTemplate(OrderConstants.TYPE_NAME, Arrays.asList(getOriginalTemplateName()), getCriteriaMap(primaryRow), Collections.emptyList()); internally has a call to other method called getCriteriaMap(primaryRow), this method has a validation error in it when it does not a find a value. I was not passing this expected value for the method. Once the value was passed. My method which was mocked or stubbed started working – Avinash Reddy Feb 03 '16 at 02:55
  • Glad that worked out for you. – Lencalot Feb 04 '16 at 18:16

0 Answers0