java - Not able to return value when using the following methods Mockito.when(), PowerMockito.when(),Mockito.doReturn() -
following classes
i have interface following method.
the interface follows , has 1 of method
public interface simpledocumentmanager{ public list getuserdocidofrelateddocumentsfortemplate(string doctype, list<string> templatenames,zonecriteria mainzonecriteria,list<zonecriteria> detailzonecriteria); }
note : zonecrieria pojo class
i have abstract super class , have init method in it, have sub class extends super class , overrides init method, , in sub class init method call respective service method. code following classes 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 above method zonecriteria have written private method in sub class
private zonecriteria getcriteriamap(){ logic returns zonecriteria. }
the above method used 1 of parameter returns zonecriteria when call method present in simpledocumentmanager.
and here test case 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 3 ways return value when method in simpledocumentmanager called. following 3 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 call returns me value. when debug code getsimpledocumentmanager() returns mocked object, method not called.
also have created subclass object in 2 ways. follows
1. directly instantiated subclass in test method follows subclass subclass = new subclass(); subclass.init(); 2.did spy of instantiated subclass subclass subclass = new subclass(); subclass = mockito.spy(subclass ); subclass .init();
when method getsimpledocumentmanager().getuserdocidofrelateddocumentsfortemplate(orderconstants.type_name, arrays.aslist(getoriginaltemplatename()), getcriteriamap(primaryrow), collections.emptylist())
is called want return "list<map<string, object>>"
doing
map<string, object> map = new hashmap<string, object>(); map.put("user_doc_id", "por0000001"); list<map<string, object>> aslist = arrays.aslist(map);
and returning aslist in thenreturn() follows thenreturn(aslist);
while preparing zonecriteria check value , if value not present have userdefined exception thrown.
so when execute test case validation exception instead should return list have prepared.
i have gone through different links in stack overflow , not answer. following links. link1
please help
Comments
Post a Comment