Showing posts with label mock. Show all posts
Showing posts with label mock. Show all posts

Friday, March 22, 2013

Testing plugins with Ivy using Powermock

Recently, liferay-plugins SDK (and also its EE version) switched to using Ivy to manage some of the dependencies. Even when there are still static dependencies taken from defined portal -- Ivy dependencies are added to the lib/ext and ROOT/WEB-INF/lib ones, see build-common.xml -- this makes adding dependencies into plugins so much easier and cleaner.

Since I'm working on some plugins, I wanted to cover the functionality properly using JUnit tests, using  Powermock and Mockito. Here are necessary steps to get those libraries on test classpath in plugins SDK:

(1) open ./ivy.xml
(2) add following dependencies:

...
<dependency conf="runtime" name="xml-apis" org="xml-apis" rev="1.4.01" />
<dependency conf="test" name="junit" org="junit" rev="4.11" />
<!-- CUSTOM BEGIN -->
<dependency conf="test" name="mockito-all" org="org.mockito" rev="1.9.5" />
<dependency conf="test" name="powermock-api-mockito" org="org.powermock" rev="1.5" />
<dependency conf="test" name="powermock-api-support" org="org.powermock" rev="1.5" />
<dependency conf="test" name="powermock-core" org="org.powermock" rev="1.5" />
<dependency conf="test" name="powermock-reflect" org="org.powermock" rev="1.5" />
<dependency conf="test" name="powermock-module-junit4" org="org.powermock" rev="1.5" />
<dependency conf="test" name="powermock-module-junit4-common" org="org.powermock" rev="1.5" />
<dependency conf="test" name="javassist" org="javassist" rev="3.12.1.GA"/>
<!-- CUSTOM END -->
...

I'm sure there may be more combinations of dependencies, this is the first that fully worked for my basic tests using classes and calls like @PrepareForTest@RunWith(PowerMockRunner.class)PowerMockito.mock(...) and Mockito.*.

Tuesday, February 5, 2013

Creating unit tests in core Liferay sources

Liferay has been catching up lately on the tests coverage. Many improvements have been made in the trunk located in liferay-portal / master branch on GitHub and since I've been fixing some things recently, I've included unit test for the changed class as well.

Quick start how-to:
  1. read Liferay testing documentation to get the basic picture,
  2. create your unit test in portal-impl/test/unit/.../<your_class>Test.java,
  3. create test methods as usual with JUnit 4.x,
  4. run the test and see results:
    • cd portal-impl,
    • ant test-class -Dclass=CustomJspRegistryImplTest,
    • note that only the name of the class is used, not the full name including package.
Testing just a single class at a time seems to be necessary, executing ant test-unit to run all existing unit tests seems to be time consuming (from minutes to tens of minutes).