Hybris Mockito:获取异常没有用于(分离的)模型的 LocaleProvider
Hybris Mockito : Getting Exception there is no LocaleProvider for (detached) model
我正在我的 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)
这是一个测试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));
}
在这里你可以看到我正在创建 ClassificationAttributeModel
的实例并设置它的代码和名称。这里的名称是本地化的,所以我也给了 Locale.English。但是每当我 运行 这个,我就没有 LocaleProvider 异常。
实际上在我的外观中,无论在哪里调用 attributeValue.getName()
,都会得到相同的异常。
例如:
dto.setName(classificationModel.getName());
我们不能忽略这种语言环境行为吗?或者对此的替代解决方案是什么。请帮忙。
找到解决方案....
我必须使用 StubLocaleProvider
设置 LocaleProvider,然后将其分配给 ItemModelContext。
import
de.hybris.platform.servicelayer.internal.model.impl.LocaleProvider;
LocaleProvider localeProvider = new
StubLocaleProvider(Locale.ENGLISH);
测试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");
我正在我的 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)
这是一个测试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));
}
在这里你可以看到我正在创建 ClassificationAttributeModel
的实例并设置它的代码和名称。这里的名称是本地化的,所以我也给了 Locale.English。但是每当我 运行 这个,我就没有 LocaleProvider 异常。
实际上在我的外观中,无论在哪里调用 attributeValue.getName()
,都会得到相同的异常。
例如:
dto.setName(classificationModel.getName());
我们不能忽略这种语言环境行为吗?或者对此的替代解决方案是什么。请帮忙。
找到解决方案....
我必须使用 StubLocaleProvider
设置 LocaleProvider,然后将其分配给 ItemModelContext。
import de.hybris.platform.servicelayer.internal.model.impl.LocaleProvider;
LocaleProvider localeProvider = new StubLocaleProvider(Locale.ENGLISH);
测试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");