Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
pooja mahajan
Junit Examples
February 26, 2013 10:49 PM
Answer

pooja mahajan

Rank: Junior Member

Posts: 45

Join Date: January 29, 2013

Recent Posts

Can anyone please help me with junit test cases for any portlet Controller.
I am unable to create proxy [mock objects ] for PortalUtil.
Issue:
@Mock HttpServletRequest httpRequest;
@Mock PortletRequest portletRequest ;

@Before
public void setUp() throws Exception {
when(PortalUtil.getHttpServletRequest(portletRequest)).thenReturn(httpRequest);

}

above line is giving me null pointer exception
pooja mahajan
RE: Junit Examples
February 28, 2013 11:34 PM
Answer

pooja mahajan

Rank: Junior Member

Posts: 45

Join Date: January 29, 2013

Recent Posts

Can anyone please help:
We are using liferay 5.2 + junit 4
want to write junit test case:

Isuues:

@Before
public void setUp() throws Exception {

InitUtil.initWithSpring();
}

Error:

java.lang.NoClassDefFoundError: javax/servlet/jsp/JspException
at com.liferay.portal.kernel.util.JavaProps.<init>(JavaProps.java:114)
at com.liferay.portal.kernel.util.JavaProps.<clinit>(JavaProps.java:119)
at com.liferay.portal.util.InitUtil.init(InitUtil.java:115)
at com.liferay.portal.util.InitUtil.initWithSpring(InitUtil.java:130)
Sushil Saini
RE: Junit Examples
March 1, 2013 2:52 AM
Answer

Sushil Saini

Rank: Regular Member

Posts: 104

Join Date: July 27, 2011

Recent Posts

Hi Pooja,

I am using the mockito liberary for Junit testing and creating the mock objects in the following manner. And it is working fine for me.

PortletConfig mockConfig = mock(PortletConfig.class);
PortletContext mockContext = mock(PortletContext.class);

when(mockConfig.getPortletContext()).thenReturn(mockContext);



Cheers
pooja mahajan
RE: Junit Examples
March 5, 2013 5:21 AM
Answer

pooja mahajan

Rank: Junior Member

Posts: 45

Join Date: January 29, 2013

Recent Posts

Thanks,
getting an nullpointerException for below:

HttpServletRequest httpRequest = PortalUtil .getHttpServletRequest(portletRequest);


when i am trying to run junit test case for a method in controller....
please help
Sushil Saini
RE: Junit Examples
March 5, 2013 10:33 AM
Answer

Sushil Saini

Rank: Regular Member

Posts: 104

Join Date: July 27, 2011

Recent Posts

Please try the below sample code ....

Portal portal = mock(Portal.class);
HttpServletRequest httpRequest = mock(HttpServletRequest.class);

new PortalUtil().setPortal(portal);
when(portal.getHttpServletRequest(eq(renderRequest))).thenReturn(httpRequest);


HttpServletRequest originalHttpServletRequest = mock(HttpServletRequest.class);
when(portal.getOriginalServletRequest(eq(httpRequest))).thenReturn(originalHttpServletRequest);
pooja mahajan
RE: Junit Examples
March 7, 2013 12:58 AM
Answer

pooja mahajan

Rank: Junior Member

Posts: 45

Join Date: January 29, 2013

Recent Posts

Thanks for helping me...
Suggestion provided by you is working....
pooja mahajan
RE: Junit Examples
May 2, 2013 1:59 AM
Answer

pooja mahajan

Rank: Junior Member

Posts: 45

Join Date: January 29, 2013

Recent Posts

Hi can anyone please help me to mock :

// httpReq
HttpServletRequest httpRequest=mock(HttpServletRequest.class);

// how to mock or skip following call
AuthTokenUtil.getToken(httpRequest) ;

for junit testcases.
pooja mahajan
RE: Help for Junit for liferay 5.2
May 5, 2013 11:45 PM
Answer

pooja mahajan

Rank: Junior Member

Posts: 45

Join Date: January 29, 2013

Recent Posts

Hi,

Can anyone please help me to mock /execute following in junit:

UserLocalServiceUtil.getUserById(userId).getRoles()

I was unable to mock UserLocalServiceUtil :
Sushil Saini
RE: Junit Examples
May 8, 2013 4:12 PM
Answer

Sushil Saini

Rank: Regular Member

Posts: 104

Join Date: July 27, 2011

Recent Posts

Hi Pooja,

Can you check the below link ... it have the example of mocking UserLocalServiceUtil.

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+use+JUnit+to+test+service+in+portlets/maximized

Cheers
Sushil Saini