我正在 Hybris 项目中编写外观级别的测试用例。我正在创建模型实例并设置名称和代码。模型具有一些属性 本地化,因此我得到了no LocaleProvider
异常。
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)
这是一个测试类
@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));
}
在这里,您可以看到我正在创建ClassificationAttributeModel
的实例并设置其代码和名称。这里的名称是本地化的,所以我也给出了Locale.English。但是每当我运行它时,我都会收到没有区域设置提供程序异常。
实际上在我的门面上,无论在哪里打电话attributeValue.getName()
,都会得到同样的例外。
例如:
dto.setName(classificationModel.getName());
我们不能忽略这种区域设置行为或替代解决方案是什么吗?请帮忙。
得到了解决方案....
我必须使用 StubLocaleProvider
设置 LocaleProvider,然后将其分配给 ItemModelContext。
进口 de.hybris.platform.servicelayer.internal.model.impl.LocaleProvider;
区域设置提供程序 localeProvider = new StubLocaleProvider(Locale.ENGLISH);
测试类 -
ClassificationAttributeModel classificationAttributeModel = new ClassificationAttributeModel();
LocaleProvider localeProvider = new StubLocaleProvider(Locale.ENGLISH);
ItemModelContextImpl itemModelContext = (ItemModelContextImpl) classificationAttributeModel.getItemModelContext();
itemModelContext.setLocaleProvider(localeProvider);
classificationAttributeModel.setCode("Procedure");
classificationAttributeModel.setName("Procedure");