我正在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。但是,每当我运行此程序时,都会收到No LocaleProvider Exception。其实在我的门面,无论在哪里调用
attributeValue.getName()
,都会得到相同的异常。例如 :
dto.setName(classificationModel.getName());
我们不能忽略这种语言环境行为,或者对此的替代解决方案是什么。请帮忙。
最佳答案
得到了解决方案...
我必须使用StubLocaleProvider
设置LocaleProvider,然后将其分配给ItemModelContext。
测试类-
ClassificationAttributeModel classificationAttributeModel = new ClassificationAttributeModel();
LocaleProvider localeProvider = new StubLocaleProvider(Locale.ENGLISH);
ItemModelContextImpl itemModelContext = (ItemModelContextImpl) classificationAttributeModel.getItemModelContext();
itemModelContext.setLocaleProvider(localeProvider);
classificationAttributeModel.setCode("Procedure");
classificationAttributeModel.setName("Procedure");