Quantcast
Channel: iPROFS Technology Blog » mcbeelen
Viewing all articles
Browse latest Browse all 8

Mock liferay services in unit tests for 6.1.1

$
0
0

Using mock Liferay 6.1.1 services in a unit test.

When you are developing components, which interact with Liferay services, you will want / need to use mocks for those services.
In previous versions of Liferay the best strategy for this was to use the LocalServiceUtils in your production code and provide the mocks by setting that mockLocalService on an instance of the LocalServiceUtil.

For instance: To use a mocked GroupLocalService you needed to do the following in your test setup:

GroupLocalServiceUtil groupLocalServiceUtil = new GroupLocalServiceUtil();
groupLocalServiceUtil.setService(mockGroupLocalService);

After an update to 6.1.1

Since the 6.1.1 (CE) release of Liferay on a lot of the LocalServiceUtil classes the setService()-method has been deprecated. Unfortunately it isn’t just deprecated: The implementation has been changed as well, which causes the set LocalService to be ignored. The getService in the LocalServiceUtil has been changed as well to use a service from a PortalBeanLocatorUtil or a ReferenceRegistry

Therefor in order to make the mocks available to your production code under test you need to inject a mockBeanLocator into the PortalBeanLocatorUtil and have that return your mocked LocalServices.

PortalBeanLocatorUtil.setBeanLocator(mockBeanLocator);
when(mockBeanLocator.locate(GroupLocalService.class.getName()))
    .thenReturn(mockGroupLocalService);

Tagged: liferay, mockito, unittesting

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles



Latest Images