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.
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(" + |
|
"'are-you-sure-you-want-to-delete-this'" + |
|
")) { " + |
|
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); |
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.
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
Classpath, User Entries -> Maven Dependencies
before liferay-portal-jsp-unit-test
Source, Source Lookup Path -> portal-master
before Default
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/