2

I wrote a Liferay service.

It does complex things with many different expected outputs depending on the inputs. I want to test the outputs with all of these inputs.

My service is used by several portlets, so I could manually test using the portlets, but it would take several days to test all situations, and I have to do that every week. Moreover, some inputs are not reachable using the current portlets.

In the implementation module of my service, I created JUnit tests, but they fail because they can not find OSGi classes:

enter image description here

Now I realize that a Liferay service can not really live on its own without the OSGi+Liferay infrastructure to support it behind, give it access to other services and data.

So, I am OK with running my tests within a live Liferay instance rather than in Eclipse. I am OK with my unit tests being launched by a Gogo Shell command, a REST call, an ad-hoc portlet, or even on module activation.

Question: How to implement such "unit" tests for my Liferay service?

Note: Unit tests for Liferay portlets are covered by another question.

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • So to make it more clear .. I think you are not asking about a unit test but rather an integration or system test. (Even if it is written in junit) – Christian Schneider Jul 18 '19 at 09:03
  • @ChristianSchneider: Yes I guess, sorry for the wording! There are many tests, each testing a small little edge case, and I want them to be execute automatically one after the other. The end result for me should be a kind of report saying that all tests succeeded (or telling me to check the logs because some tests failed). – Nicolas Raoul Jul 18 '19 at 09:12
  • Junit is usually a great tool for this but for such higher level test you usually need additional toolings. See below in my answer. – Christian Schneider Jul 18 '19 at 09:13
  • @NicolasRaoul what kind of liferay service did you write ? direct osgi ds or service builder services ? service builder supports direct remote service generation .. that's why im asking – André Jul 18 '19 at 19:05
  • @André: I wrote it from scratch, but next time I could use Service Builder if that helps. I tagged the question "liferay-service-builder" because there is no "liferay-service" tag. Are you suggesting I could write a remote service, and test the remote service via a generic REST testing tool? Great idea! The remote service would need to be in a separate module, so that I can remove that module when going to production (it would be a security risk to leave these services remotely accessible). Please post an answer detailing that strategy, thanks :-) – Nicolas Raoul Jul 19 '19 at 02:20

1 Answers1

2

I am not a liferay expert but I can describe the general approach for such tests. You need these steps:

  • build your module(s)
  • Integrate your modules into liferay system
  • Run the liferay system together with your new bundles
  • Start your junit test
  • Your unit test will interact with the running system (typically over REST or soe other remote interface)
  • After your test finishes you want to destroy the liferay instance

As liferay is an OSGi system possibly the integration step will happen after liferay is started by uploading your bundles.

This might be helpful:

If you can start liferay as a docker image then the TestContainers project might be helpful. It takes care of starting and stopping docker containers from java junit code in a safe way. For example it makes sure the containers are always cleaned up in the end even if your test crashes.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • I agree, if it is api several tools exist for that..as described. A liferay service though is is kind of a complex testing scenario. To start simple, one could embed the tests in a bundle and use the activation phase to start the tests. It is not nice and clean like the IDE, but will get it done. you can even lunch a separated thread for your runner and collect the results latter. This is a quite simple approach one can use to test a xxxLocalService instance, as inside the OSGi environment your test can depend on that component to run, keeping things in order. – Victor Jul 18 '19 at 17:50
  • For OSGi tests in reasonably simple environments I use pax exam a lot. Setting up liferay this way might be difficult though. – Christian Schneider Jul 19 '19 at 19:34
  • Is there some other alternative? I am concerned about affecting build performance by requiring a Liferay instance to be stood up via Arquillian. – Byob Jan 08 '21 at 12:35