
JUnit
Table of Contents [-]
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.ServiceTestUtilis 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.propertieswith 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)