Giter Site home page Giter Site logo

liferay-portal-jsp-unit-test's Introduction

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/

liferay-portal-jsp-unit-test's People

Contributors

arboliveira avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

adolphoramirez

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.