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
@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
Please help
Aucun commentaire:
Enregistrer un commentaire