6

我正在我的 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());

我们不能忽略这种语言环境行为或替代解决方案是什么。请帮忙。

4

1 回答 1

12

得到解决方案....

我必须使用设置 LocaleProvider StubLocaleProvider,然后将其分配给 ItemModelContext。

导入 de.hybris.platform.servicelayer.internal.model.impl.LocaleProvider;

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");
于 2015-04-28T05:26:00.487 回答