Skip to content

Latest commit

 

History

History
60 lines (31 loc) · 1.96 KB

README.md

File metadata and controls

60 lines (31 loc) · 1.96 KB

Liferay Portal JSP Unit Test examples

How to test actual rendered HTML from your Portal JSP pages and taglibs, without the need to start a web container.

Highlights

JSP: assert rendered HTML

@Test
public void test1Comment() throws Exception {
testCommentCount(1, "1 comment");
}
@Test
public void testNComments() throws Exception {
testCommentCount(142857, "142857 comments");
}
protected void testCommentCount(int commentCount, String rendered)
throws Exception {
when(
blogsJSP.commentManager.getCommentsCount(
anyString(), anyLong())).thenReturn(commentCount);
ResponseContent response = engine.execute();
response.assertContains(
"<a href=\"portletURL#blogsCommentsPanelContainer\" >"
+ rendered
+ "</a>");
}

Taglibs: assert rendered HTML

response.assertContains("<textarea class=\"field form-control\" " +
"id=\"" + randomNamespace + "editReplyBody" + messagesCount +
"\" " +
"name=\"editReplyBody" + messagesCount + "\" " +
"title=\"reply-body\" wrap=\"soft\" style=\"height: 100px;\" >" +
body +
"</textarea>");
response.assertLookingAt(".*" +
Pattern.quote(
"<a href=\"javascript:if (confirm(" +
"&#039;are-you-sure-you-want-to-delete-this&#039;" +
")) { " +
randomNamespace +
"deleteMessage(" +
messagesCount +
"); } else { self.focus(); }\" " +
"class=\" taglib-icon\" id=\"") +
"........" +
Pattern.quote(
"\" >" +
"<i class=\"icon-remove\"></i>" +
"<span class=\"taglib-text \">delete</span>"));

Portal page rendering: convenience mocks

private void setUpLanguageUtil() {
Language language = mock(Language.class);
when(
language.get((HttpServletRequest)any(), anyString())).then(
returnArgumentAsIs(1));
when(
language.get((Locale)any(), anyString())).then(
returnArgumentAsIs(1));
new LanguageUtil().setLanguage(language);
}

Portal infrastructure: convenience mocks

liferayPortletRequest = mock(LiferayPortletRequest.class,
withSettings().extraInterfaces(RenderRequest.class));
liferayPortletResponse = mock(LiferayPortletResponse.class,
withSettings().extraInterfaces(RenderResponse.class));
ServletTester tester = engine.getServletTester();
tester.setAttribute("liferayPortletRequest", liferayPortletRequest);
tester.setAttribute("liferayPortletResponse", liferayPortletResponse);
tester.setAttribute("themeDisplay", themeDisplay);
tester.setAttribute("locale", Locale.US);

How to use

Your workspace must have a working "portal-master" project.

Eclipse, "Import", "Existing Maven project".

Run the tests using the liferay-portal-jsp-unit-test launch configuration.

Under the hood

If you ever need to configure a new Liferay Portal JSP Unit Test project from scratch.

Eclipse project, Java Build Path

Required projects in the build path -> portal-master

Libraries -> portlet.jar - portal-master/lib/global

Launch configuration

Classpath, User Entries -> Maven Dependencies before liferay-portal-jsp-unit-test

Source, Source Lookup Path -> portal-master before Default

Acknowledgements

Adapted from work by Günther Enthaler, who wrote the original Jetty JSP unit test simple example.

https://github.com/genthaler/jetty-jsp-unit-test-simple/