Foren

JUnit - Liferay throwing NPE

Ketan Solanki, geändert vor 6 Jahren.

JUnit - Liferay throwing NPE

Junior Member Beiträge: 63 Beitrittsdatum: 28.05.14 Neueste Beiträge
Hi All,

I want to write JUnit test cases for my code in Liferay and hence I am using Easymock + Power mock for this. I am facing issue when I want to mock a few objects where internal method variables are coming as null.

e.g. below would throw NPE because PortalUtil.getHttpServletRequest uses _portal variable which is private to PortalUtil class


HttpServletRequest httpServletRequest = EasyMock.createMock(HttpServletRequest.class);
HttpServletResponse httpServletResponse = EasyMock.createMock(HttpServletResponse.class);

EasyMock.expect(PortalUtil.getHttpServletRequest(actionRequest)).andReturn(httpServletRequest);
EasyMock.expect(PortalUtil.getHttpServletResponse(actionResponse)).andReturn(httpServletResponse);



java.lang.NullPointerException
	at com.liferay.portal.kernel.util.PortalUtil.getHttpServletRequest(PortalUtil.java:1007)
	at my.package.HomePagePortletTest.testSavePageShortTerm(HomePagePortletTest.java:53)



In this as well the same issue
ServiceContext serviceContext = ServiceContextFactory.getInstance(Group.class.getName(), actionRequest);




java.lang.NullPointerException
	at com.liferay.portal.kernel.service.ServiceContextFactory.getInstance(ServiceContextFactory.java:306)
	at com.liferay.portal.kernel.service.ServiceContextFactory.getInstance(ServiceContextFactory.java:549)
	at my.package.LiferayInstanceServiceTest.testCreateSite(LiferayInstanceServiceTest.java:39)


I checked online (https://stackoverflow.com/questions/3162551/how-do-i-mock-static-methods-in-a-class-with-easymock) and they suggested to use PowerMock static method mocking but somehow that's not working in my case.

Please note that with the same approach,
EasyMock.expect(ParamUtil.getString(actionRequest, "phone")).andReturn("123456789");
static method mocking is working perfectly fine because internally it's not using any variables which are NULL

If you have any idea then please share.

Thanks,
Ketan
Ketan Solanki, geändert vor 6 Jahren.

RE: JUnit - Liferay throwing NPE

Junior Member Beiträge: 63 Beitrittsdatum: 28.05.14 Neueste Beiträge
Answering my own question as I found an answer and for the sake of others if they every stumble upon this question!

Use below

@SuppressStaticInitializationFor("com.liferay.dynamic.data.mapping.service.DDMStructureLocalServiceUtil")


the fully qualified class name where static initializer are lying.This will suppress all of them (above class wouldn't be the exact match for the problem mentioned in the main question but it will give you indications).

BTW, please check another related questions posted at PowerMock.expectPrivate behavior to broader audience because that's not related to Liferay actually but has sprung up only while working on liferay test cases. In liferay, we have lots of *Util classes and hence if anyone wants to write test cases then they will definitely need solution for the question that I have asked in.

Thanks,