Code Monkey home page Code Monkey logo

liferay-test's Introduction

liferay-test

Testing portlets with services

Right now it can't be easily done, as both portlet and portal context are configured with similarly named files (base-spring.xml, ...) and portlet xmls take priority.

TODO: can easyconf be instructed to return paths to files in portal-impl jar?

Best bet is to:

  • copy portal Spring context configuration files (portal-impl/META-INF/*-spring.xml) to test path META-INF (test/unit/META-INF/portal/*-spring.xml),
  • copy liferay-test/src/main/resources/portal-ext.properties to test path classpath (test/unit/portal-ext.properties),
  • copy spring.configs property from portal-impl/portal.properties to test/unit/portal-ext.properties and replace META-INF/ with META-INF/portal/,
  • remove the persistence implementation you don't use (most likely jpa-spring.xml),
  • comments in this property might cause an exception,
  • including ext-spring.xml might cause an exception.

In your @BeforeClass method do ctx.initializePortletServices("guestbook-portlet");. This creates a spring context for the portlet and caches the PortletBeanLocator instance.

Usage

Use PortalInitializer.initialize() to start portal with all services:

public class NewPortletTest
{
	private NewPortlet newPortlet; // MVCPortlet
	private static MVCPortletContext ctx;

	@BeforeClass
	public static void beforeClass() throws ReflectiveOperationException, SystemException
	{
		PortalInitializer.initialize();
		ctx = new MVCPortletContext();
		ctx.initializePortletServices("guestbook-portlet");
	}

	@Before
	public void before()
	{
		newPortlet = new NewPortlet();
		PortletContext pctx = new TestPortletContext("test");
		PortletConfig pcfg = new TestPortletConfig(pctx);
		newPortlet.init(pcfg);
	}

    /**
     * Render methods are not fully supported yet - PortletRequestDispatcher implementation missing.
     */
	@Test
	public void renderTest() throws PortletException, IOException, ReflectiveOperationException
	{
		RenderRequest req = PortletRequestBuilder.newRenderRequest()
				.withWindowState(WindowState.NORMAL)
				.withPortletMode(PortletMode.VIEW)
				.withPortlet(ctx.getPortlet())
				.withThemeDisplay(ctx.getThemeDisplay())
				.build();
		RenderResponse resp = PortletResponseBuilder.newRenderResponse()
				.withPortletRequest(req)
				.build();

		newPortlet.render(req, resp);
	}

	@Test
	public void actionTest() throws ReflectiveOperationException, PortalException, SystemException, IOException, NamingException, SQLException
	{
		ActionRequest req = PortletRequestBuilder.newActionRequest()
				.withPortlet(ctx.getPortlet())
				.withThemeDisplay(ctx.getThemeDisplay())
				.withUser(ctx.getUser())
				.withLayout(ctx.getLayout())
				.withParameters(ParametersBuilder.builder().withParameter("name", "value").build())
				.build();
		ActionResponse resp = PortletResponseBuilder.newActionResponse()
				.withPortletRequest(req)
				.build();

		int count = EntryLocalServiceUtil.getEntriesCount();

		// addGuestbook reads parameters and creates a new Entry
		newPortlet.addGuestbook(req, resp);

		int count2 = EntryLocalServiceUtil.getEntriesCount();
		Assertions.assertThat(count2).isGreaterThan(count);
	}
}

If portal startup is too slow for unit tests, try creating service mocks (with Mockito, for instance) and use ServiceUtil#rewireService to wire them:

liferay-test's People

Contributors

kbrus avatar

Watchers

 avatar

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.