Hybris Mockito:获取异常,没有(分离的)模型的 LocaleProvider

Posted

技术标签:

【中文标题】Hybris Mockito:获取异常,没有(分离的)模型的 LocaleProvider【英文标题】:Hybris Mockito : Getting Exception there is no LocaleProvider for (detached) model 【发布时间】:2015-07-06 18:55:27 【问题描述】:

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

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

【问题讨论】:

【参考方案1】:

找到解决方案....

我必须使用 StubLocaleProvider 设置 LocaleProvider,然后将其分配给 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");

【讨论】:

以上是关于Hybris Mockito:获取异常,没有(分离的)模型的 LocaleProvider的主要内容,如果未能解决你的问题,请参考以下文章

SAP Hybris电商系统与阿里云联合测试系列十读写分离

mock单元测试 mockito实践

从hybris平台获取数据

Hybris 初始化期间出现错误的 SQL 语法异常

使用 Junit 和 Mockito 嵌套异常问题测试 POST Api

的Mockito。没有捕获参数值