i writing test cases @ facade level in hybris project. creating model instance , setting name , code. model having attributes localized, because of getting no localeprovider
exception.
java.lang.illegalstateexception: there no localeprovider (detached) model de.hybris.platform.servicelayer.model.itemmodelcontextimpl@66c677a7 @ de.hybris.platform.servicelayer.model.itemmodelcontextimpl.getlocaleprovider(itemmodelcontextimpl.java:481) @ de.hybris.platform.servicelayer.model.itemmodelcontextimpl.getcurrentlocale(itemmodelcontextimpl.java:469) @ de.hybris.platform.servicelayer.model.itemmodelcontextimpl.todatalocale(itemmodelcontextimpl.java:406) @ de.hybris.platform.servicelayer.model.itemmodelcontextimpl.getlocalizedvalue(itemmodelcontextimpl.java:323) @ de.hybris.platform.catalog.model.classification.classificationattributemodel.getname(classificationattributemodel.java:227) @ de.hybris.platform.catalog.model.classification.classificationattributemodel.getname(classificationattributemodel.java:217)
here test class
@runwith(powermockrunner.class) @preparefortest({ locale.class, config.class }) public class hccb2bclassificationfacadeunittest { @injectmocks private final hccb2bclassificationfacade hccb2bclassificationfacade = new hccb2bclassificationfacadeimpl(); @mock hccb2bclassificationservice hccb2bclassificationservice = new hccb2bclassificationserviceimpl(); @mock private sessionservice sessionservice; @mock private hccb2bclassificationdao hccb2bclassificationdao; @mock private searchrestrictionservice searchrestrictionservice; @before public void setup() throws systemexception { mockitoannotations.initmocks(this); classattributeassignmentmodel classattributeassignmentmodel = new classattributeassignmentmodel(); classificationattributemodel classificationattributemodel = new classificationattributemodel(); classificationattributemodel.setcode("procedure"); classificationattributemodel.setname("procedure",locale.english); //localized variable classattributeassignmentmodel.setclassificationattribute(classificationattributemodel); list<classattributeassignmentmodel> classattributeassignmentmodellist=new arraylist<classattributeassignmentmodel>(); classattributeassignmentmodellist.add(classattributeassignmentmodel); mockito.doreturn(null).when(sessionservice).getattribute("attributedtolist"); mockito.when(hccb2bclassificationservice.getclassattributeassignmentlist(classificationattributelevel.level1,"fac001")).thenreturn(classattributeassignmentmodellist); } @test public void getclassificationattributelist() { hccb2bcategoryparameter categoryparamter = new hccb2bcategoryparameter(); categoryparamter.setcategorycode("fac001"); assert.assertnotnull(hccb2bclassificationfacade.getclassificationattributelist(categoryparamter)); }
here can see creating instance of classificationattributemodel
, setting code , name of that. here name localized have given locale.english well. whenever running this, getting no localeprovider exception.
actually in facade, wherever calling attributevalue.getname()
, getting same exception.
for example :
dto.setname(classificationmodel.getname());
can't ignore locale behavior or alternative solution that. please help.
got solution....
i have set localeprovider using stublocaleprovider
, assign itemmodelcontext.
import de.hybris.platform.servicelayer.internal.model.impl.localeprovider; localeprovider localeprovider = new stublocaleprovider(locale.english);
test class -
classificationattributemodel classificationattributemodel = new classificationattributemodel(); localeprovider localeprovider = new stublocaleprovider(locale.english); itemmodelcontextimpl itemmodelcontext = (itemmodelcontextimpl) classificationattributemodel.getitemmodelcontext(); itemmodelcontext.setlocaleprovider(localeprovider); classificationattributemodel.setcode("procedure"); classificationattributemodel.setname("procedure");