Unit Test only possible for portal, not for portlets with services? {Solve

thumbnail
1693666, modified 16 Years ago. Junior Member Posts: 76 Join Date: 12/3/08 Recent Posts
In the portal source code, there are several test classes. (portal-impl/test/com/liferay/counter/service/CounterServiceTest.java)

The most interesting class is : BaseTestCase.

In this class the environment is setup to make unit testing possible. (Spring beans are initialized and Jetty is started up.)
The method which does all the work is : InitUtil.initWithSpring().

I want to unit test the business logic from the services (classes created by the service-builder) from my portlet so that I don't need a fully working Liferay environment to test some small functionality.

The question is : How can I correctly initialize the spring beans from my own portlet ?
Because this is needed to solve the "BeanLocator is null" exception.
thumbnail
1693666, modified 16 Years ago. Junior Member Posts: 76 Join Date: 12/3/08 Recent Posts
I found a solution!.
(It took a lot of searching + developing time to solve this issue, and as far as I know, this is the only solution present in the whole Liferay community!)

I did the following:

1. Create an own test project based on the portal-src from Liferay.
2. Remove all java files but keep all jar files.
3. Add portal-impl.jar, portal-kernel.jar, portal-service.jar and util-java.jar to this project.
4. Create an own version of initWithSpring() which initializes my portlet.
5. Update the build file from the services-portlet project to create a jar file which includes all classes + configuation files. (xml + properties)

See Services-Portlet project and the Test Services-Portlet project for more details.
3503231, modified 16 Years ago. Regular Member Posts: 149 Join Date: 7/7/09 Recent Posts
Hi,

Can you please explain in detail what you have followed the approach.
Actually I want to increment the liferay counter value by using the myeclipse.

Thanks in advance

Regards,
K.chandra sekhar
thumbnail
4559105, modified 16 Years ago. New Member Posts: 15 Join Date: 2/3/10 Recent Posts
For the benefits of those, who, like me, stumbled across the problem of unit testing Liferay portlets, I published a Blog article on the subject at: Liferay Portlet Unit testing.

It outlines a simple and effective solution to this problem. Feedback and comments are welcome.

Laurent