JUnit

标签: liferay junit

Writing JUnits #

When writing JUnits for Liferay, it is important to remember that Liferay is a server platform that leverages many technologies and introduces many levels of abstraction to prevent clashing of classloaders and other things of that sort. Most JUnits will need to ensure that authentication is enabled and all the services (especially Spring) is properly initialized. This is what

com.liferay.portal.service.ServiceTestUtil
is for. It is therefore good practice, whether it be in your TestCase or your TestSetup, to have the following:

	public void setUp() {
		ServiceTestUtil.initServices();
		ServiceTestUtil.initPermissions();
	}
	...
	public void tearDown() {
		ServiceTestUtil.destroyServices();
	}

Running portal-impl JUnits #

Two files need to be configured. In

<portal>/portal-impl/test/portal-test.properties
, configure your database credentials and, for some operating systems (e.g., OS X), you need to specify something for
liferay.home=
. Update
<portal>/portal-impl/test/test-portal-impl.properties
with appropriate values for companyId, userId and password.

To run a specific suite or test, go to your command prompt and, while in the portal-impl directory, type:

ant test-class -Dclass=MiscTestSuite

This works for both test suites and individual test classes as well:

ant test-class -Dclass=XmlRpcParserTest

Debugging JUnit tests #

If you want to debug your code while executing Junit tests in Liferay, you have to follow these steps:

1. Add this line to your build.<username>.properties file:

junit.debug=true

2. Launch your test as described in the previous section. You will now see a message "Listening at port XXXX"

3. In your IDE, create a socket listening to that port. For example, in IntellIJ:

  • Open Run/Debug configurations
  • Select Remote and click on the Add Icon.
  • Enter a name for your new socket (e.g. TestSocket), fill in the host and the port and click OK
  • Now you can see your new Socket at the Run/Debug configurations list.

4. Select your socket as if it were your server and start it in debug mode

At this point, the code execution will stop at the breakpoints you have defined

Other #

  • JUnits in Plugins does not seem to work (See LEP-6806)
0 附件
40106 查看
平均 (0 票)
满分为 5,平均得分为 0.0。
评论
讨论主题回复 作者 日期
Hi Ganesh, This article should be expanded to... Jorge Ferrer 2008年7月27日 上午9:19
Sure Jorge. This is just the starting phase.... Ganesh Ram 2008年7月27日 下午11:11
Is there any way to have these kind of tests in... Alex Wallace 2008年9月30日 下午2:39
Is it work in liferay portal 5.2.3. I can't run... GoreX GoreX 2009年8月12日 上午10:55
There is no such ServiceTestUtil either in the... Adam Victor Nazareth Brandizzi 2010年4月30日 上午8:22
I have de same problem, how do a can use Junit... Joaquin Cabal 2010年6月3日 下午2:04
Bill Gates would be so proud... Jakub Liska 2011年8月27日 下午2:29
Hi, I want to create JUnits to test my... Jaime L. López Carratalá 2011年11月15日 上午4:18
Even having the ServiceTestUtil class available... Dirk Ulrich 2011年12月4日 下午11:50
Dear all, More information, please refer to: ... P.C. SUN 2012年2月22日 下午10:28
http://www.liferay.com/community/wiki/-/wiki/Ma... P.C. SUN 2012年2月22日 下午10:30

Hi Ganesh,

This article should be expanded to be really useful
在 08-7-27 上午9:19 发帖。
Sure Jorge. This is just the starting phase. Will surely expand this.

Thanks,
Ganesh
在 08-7-27 下午11:11 发帖以回复 Jorge Ferrer
Is there any way to have these kind of tests in an ext environment for custom services/code?
在 08-9-30 下午2:39 发帖。
Is it work in liferay portal 5.2.3.
I can't run the tests, beacuse it's connection refused. I created the user the database and all privileges in mysql database.
在 09-8-12 上午10:55 发帖。
There is no such ServiceTestUtil either in the sources of Liferay 5.2.3 or in the compiled Liferay. Where can I find it? Is there another way to write tests for new Liferay portlets?
在 10-4-30 上午8:22 发帖。
I have de same problem, how do a can use Junit for services in liferay 5.2.3?
在 10-6-3 下午2:04 发帖以回复 Adam Victor Brandizzi
Bill Gates would be so proud...
在 11-8-27 下午2:29 发帖以回复 Joaquin Cabal
Hi, I want to create JUnits to test my portlets, but in the EE I can't find the ServiceTestUtil or the base test classes, how can I write junit test in EE?
在 11-11-15 上午4:18 发帖以回复 Jakub Liska
Even having the ServiceTestUtil class available the description above is not sufficient enough to get unit test work.
A comprehensive guidance of how to set up the test environment would be highly appreciated.
在 11-12-4 下午11:50 发帖。
Dear all,

More information, please refer to:

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+use+JUnit+to+test+servi­ce+in+portlets
在 12-2-22 下午10:28 发帖以回复 Dirk Ulrich
http://www.liferay.com/community/wiki/-/wiki/Main/How+to+use+JUnit+to+test+servi­ce+in+portlets
在 12-2-22 下午10:30 发帖以回复 P.C. SUN