I am writing test cases at facade level in my Hybris Project. I am creating model instance and setting name and code. Model is having some attributes Localized, because of that I am getting no LocaleProvider
Exception.
java.lang.IllegalStateException: there is no LocaleProvider for (detached) model de.hybris.platform.servicelayer.model.ItemModelContextImpl@66c677a7
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getLocaleProvider(ItemModelContextImpl.java:481)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getCurrentLocale(ItemModelContextImpl.java:469)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.toDataLocale(ItemModelContextImpl.java:406)
at de.hybris.platform.servicelayer.model.ItemModelContextImpl.getLocalizedValue(ItemModelContextImpl.java:323)
at de.hybris.platform.catalog.model.classification.ClassificationAttributeModel.getName(ClassificationAttributeModel.java:227)
at de.hybris.platform.catalog.model.classification.ClassificationAttributeModel.getName(ClassificationAttributeModel.java:217)
Here is a 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 you can see I am creating instance of ClassificationAttributeModel
and setting code and name of that. Here Name is Localized so i have given Locale.English as well. But whenever I am running this, I am getting No LocaleProvider Exception.
Actually in my facade, wherever calling attributeValue.getName()
, getting the same exception.
For Example :
dto.setName(classificationModel.getName());
Can't we ignore this locale behavior or what is the alternative solution for that. Please help.
Got the Solution....
I have to set LocaleProvider using StubLocaleProvider
and then assign it to the 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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With