Foros de discusión

Configure liferay for Junit ..

thumbnail
Rojalin Patri, modificado hace 12 años.

Configure liferay for Junit ..

Expert Mensajes: 287 Fecha de incorporación: 22/03/11 Mensajes recientes
Hi All,
we have a method that creates a new user using Liferay,we need to write a Junit case for that.And in order to do that we need to replicate the Liferay environment,to test that in standalone mode i.e Junit.How can we configure liferay first for junit....
Please help..
Thanks
Rojalin
thumbnail
Sandeep Nair, modificado hace 12 años.

RE: Configure liferay for Junit ..

Liferay Legend Mensajes: 1744 Fecha de incorporación: 6/11/08 Mensajes recientes
Check the following link please

http://www.liferay.com/community/wiki/-/wiki/Main/Integrating+unit+tests+with+plugins+SDK

Regards,
Sandeep
thumbnail
Rojalin Patri, modificado hace 12 años.

RE: Configure liferay for Junit ..

Expert Mensajes: 287 Fecha de incorporación: 22/03/11 Mensajes recientes
Thanks for the reply Sandeep.i have alreday gone through the wiki link.and i am sorry i didn't mention my requirement clearly.
Actually i am testing a application developed on liferay.so one of my testcase requirement is "It has to test whether the liferay services are up or not".For that i need the server should be up and running.while doing automation testing it's not always possible to start and shut down the server.so i need somehow the startup.bat file should be invoked programatically.or we need to simulate the environment somehow so that i can check the services are up or not.
Any idea on this Sandeep??????or any suggestions how to proceed??
Thanks
Rojalin
thumbnail
Michael Wall, modificado hace 12 años.

RE: Configure liferay for Junit ..

Junior Member Mensajes: 60 Fecha de incorporación: 22/07/10 Mensajes recientes
You could have your JUnit setup method call a batch file that checks the status of the App Server service (e.g. if you are using JBoss) and stops / starts the Service as needed.

You can monitor and stop / start services on a remote machine.

Alternatively have your JUnit setup method, attempt to connect to the app server url programatically and proceed based on the response code.
thumbnail
Rojalin Patri, modificado hace 12 años.

RE: Configure liferay for Junit ..

Expert Mensajes: 287 Fecha de incorporación: 22/03/11 Mensajes recientes
Hi Michael,
thanks for the reply.it was helpful.
Thanks
Rojalin
thumbnail
Rojalin Patri, modificado hace 12 años.

RE: Configure liferay for Junit ..

Expert Mensajes: 287 Fecha de incorporación: 22/03/11 Mensajes recientes
Hi All,
finally got it worked.i wanted to test default liferay services let's say addUser() function or getUsers() function.So i did it in following way.
1.i imported junit.jar to the library folder of my project and all jars related to struts,hibernate and the jars like portal-service.jar,portal-impl.jar whose functions i am gonna test.
2.then in src folder i placed my testcase.for example:the below code in my program.
package com.liferay.JunitTest;
import org.junit.After;
import org.junit.Before;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.UserService;
import com.liferay.portal.service.UserLocalService;
import com.liferay.portal.service.ServiceContext;

import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.service.UserServiceTest;
//import com.liferay.portal.model.*;
import com.liferay.portal.model.User;
import com.liferay.portal.kernel.*;
//import com.liferay.portal.*;
import org.junit.Test;
import junit.framework.TestCase;
//import junit.framework.TestSuite;
import java.util.Locale;

import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;
import com.liferay.portal.util.InitUtil;

public class NewTest extends TestCase
{
private User user=null;
private Locale locale;
private UserService userService=null;
private ServiceContext serviceContext=new ServiceContext();
private UserServiceTest test=new UserServiceTest();

@Before
public void setUp() throws Exception {
super.setUp();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void testmyTest()throws Exception
{

System.out.println("A new Testcase");

UserLocalService localService=UserLocalServiceUtil.getService();

java.util.List<com.liferay.portal.model.User> user=localService.getUsers(0,9);
System.out.println("Number of User:"+localService.getUsersCount());

for(User u:user)
{
System.out.println("users first name:"+u.getFirstName()+"-"+u.getEmailAddress());

}

}
}
3.In src folder itself added two more property files portal-ext.properties and portal-test.properties and added the following code to the property files respectively
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=

hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_minimal_puts=false
hibernate.cache.use_structured_entries=false
..................................................................................
jdbc.default.driverClassName=com.mysql.jdbc.Driver
jdbc.default.url=jdbc:mysql://localhost/lportal?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
jdbc.default.username=root
jdbc.default.password=

value.object.listener.com.liferay.portal.model.LayoutSet=
ehcache.portal.cache.manager.jmx.enabled=false
resource.repositories.root=/home/rojalin/Desktop/liferay-portal-5.2.3/tomcat-6.0.18/webapps/ROOT(path of the tomcat bundle)
scheduler.enabled=false

hibernate.configs=\
META-INF/mail-hbm.xml,\
META-INF/portal-hbm.xml,\
META-INF/ext-hbm.xml
4.then i run the run the testcase as Junit testcase.and it compile the Testcase.java file successfully.
This may not be a significant one.but hope this will help someone in the community who would be struggling like me regarding Junit.
Thanks to the community for the inputs i have got to make it work.
thumbnail
P.C. SUN, modificado hace 12 años.

RE: Configure liferay for Junit ..

New Member Mensajes: 9 Fecha de incorporación: 18/10/11 Mensajes recientes
Hi Rojalin,

I also tried to write Junit test code and I tried your sample as well. However, I always got the error: BeanLocator is Null. Could u tell me how you handle that?

And in the project build path, I put all jar files in which are from ROOT/lib. However, I still cannot find the UserServiceTest test=new UserServiceTest(). Can you let me know whether it is useful or how we get that?

Thank you and looking forwards to your reply.

P.C.(JACK) Sun
thumbnail
Rojalin Patri, modificado hace 12 años.

RE: Configure liferay for Junit ..

Expert Mensajes: 287 Fecha de incorporación: 22/03/11 Mensajes recientes
Hi P.C,
first of all i apologize for not removing unwanted local variables and imports while posting the solution here in the thread.
and yes,u r right.this UserServiceTest test=new UserServiceTest() is not at all needed.i just imported to try it using the in built test services available inside Liferay.
Here i have just initiated the services using UserLocalService localService=UserLocalServiceUtil.getService();.
and could you please tell me where exactly you are getting this beanLocator Null problem???
thumbnail
P.C. SUN, modificado hace 12 años.

RE: Configure liferay for Junit ..

New Member Mensajes: 9 Fecha de incorporación: 18/10/11 Mensajes recientes
Hi Rojalin,

Thanks for your reply. I've already mode it work. emoticon The ext-spring.xml I put into a wrong place. And I post the update as a new wiki:

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+use+JUnit+to+test+servi­ce+in+portlets

emoticon Thanks a lot for your information, it's very helpful.

B.R.

P.C.SUN
Gautam Sharma, modificado hace 11 años.

RE: Configure liferay for Junit ..

Junior Member Mensajes: 92 Fecha de incorporación: 30/04/12 Mensajes recientes
can you please share your sample war.

It would be great help
thumbnail
P.C. SUN, modificado hace 11 años.

RE: Configure liferay for Junit ..

New Member Mensajes: 9 Fecha de incorporación: 18/10/11 Mensajes recientes
Hi Gautam,

All the related source code and config files, I've already put into that WIKI. You can refer to that and try to make it.

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+use+JUnit+to+test+service+in+portlets

I paste that URL into browser, seems not work, you can go to wiki and find that article.

Thanks.

P.C.
Gautam Sharma, modificado hace 11 años.

RE: Configure liferay for Junit ..

Junior Member Mensajes: 92 Fecha de incorporación: 30/04/12 Mensajes recientes
Hi its working fine with liferay 6.0 ce but not working in 6.1 ce any idea????
thumbnail
P.C. SUN, modificado hace 11 años.

RE: Configure liferay for Junit ..

New Member Mensajes: 9 Fecha de incorporación: 18/10/11 Mensajes recientes
Hi,

Basically, I tried that and it didn't work, seems the dependencies are changed in 6.1, as I didn't have enough time to do it further, I wish someone could help to make it work.

Thanks.
thumbnail
Marco Dussin, modificado hace 11 años.

RE: Configure liferay for Junit ..

Junior Member Mensajes: 45 Fecha de incorporación: 6/07/10 Mensajes recientes
I've got the same problem on LR 6.1 CE.

Have you resolved the "BeanLocator is null" error?

Thank you

Marco
thumbnail
Rajender Reddy Nagulapalle, modificado hace 11 años.

RE: Configure liferay for Junit ..

Junior Member Mensajes: 33 Fecha de incorporación: 21/05/11 Mensajes recientes
any one solved this issue. i am having same bean creation errors
SZ khan, modificado hace 11 años.

RE: Configure liferay for Junit ..

Regular Member Mensajes: 145 Fecha de incorporación: 31/10/09 Mensajes recientes
I don't see any working example of Junit with LIferay 6.1
thumbnail
P.C. SUN, modificado hace 11 años.

RE: Configure liferay for Junit ..

New Member Mensajes: 9 Fecha de incorporación: 18/10/11 Mensajes recientes
Hi CZ,

The JUnit way in this article is for 6.0, no idea anyone did some approach to 6.1

Did u try it? You may get some error for spring obj injection?

P.C.Sun
thumbnail
Kalai Arasan, modificado hace 7 años.

RE: Configure liferay for Junit ..

Regular Member Mensajes: 127 Fecha de incorporación: 2/01/13 Mensajes recientes
Hi,
I have tried the example in Liferay 6.2 and getting the below error.


A new Testcase
Loading jar:file:.m2/repository/com/liferay/portal/portal-impl/6.2.4/portal-impl-6.2.4.jar!/system.properties
Jun 07, 2016 9:58:47 AM com.liferay.portal.kernel.log.Jdk14LogImpl error
SEVERE: BeanLocator is null
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1365)
at com.liferay.portal.kernel.bean.PortalBeanLocatorUtil.locate(PortalBeanLocatorUtil.java:74)
at com.liferay.portal.service.UserLocalServiceUtil.getService(UserLocalServiceUtil.java:3417)
at com.junit.test.NewTest.testmyTest(NewTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)