Unit Tests - Documentation topics on: testing,unit tests,.

This documentation is a static copy for this version. For current documentation, see: http://dotcms.com/docs/latest

Unit Tests

In addition to Functional Tests and Integration Tests, dotCMS provides the ability to Unit Test each method (function) in a class, including dependencies of that method/class.

Unit tests have their own testing resources and do not require starting dotCMS prior to running the tests.

Configuring a Custom API Locator Class:

Unit Tests run against their own mocked data. The API_LOCATOR_IMPLEMENTATION property defines the class that will be called to locate all the unit testing mock data. To customize the API locator to call a different class, add this property to your dotmarketing-config.properties (via configuration plugin.):

API_LOCATOR_IMPLEMENTATION=com.dotcms.TestBase.MyAPILocator

The TestBase.java file (shown below) uses this property to set the API Locator. Setting this property during the development phase will allow you to change the data used in the automated testing of the customizations you have made to specific dotCMS features, to your own custom mock data.

package com.dotcms;

import org.junit.BeforeClass;

import com.dotmarketing.business.APILocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.BaseMessageResources;
import com.dotmarketing.util.Config;

public abstract class TestBase extends BaseMessageResources {

    public static class MyAPILocator extends APILocator {       
    }

    @BeforeClass
    public static void prepare () throws DotDataException, DotSecurityException, Exception {
        Config.initializeConfig();
        Config.setProperty("API_LOCATOR_IMPLEMENTATION", MyAPILocator.class.getName());
    }
}  

How to Run Unit Tests in Eclipse

In Eclipse, right-click on the src/test/java package, then on “Run As”, and select “JUnit Test” to run all Unit Tests, or choose one of the targeted unit tests found in the unit test suite as shown in the image below:

After running Unit Tests, a JUnit testing report tab will appear next to the console tab in Eclipse. The JUnit test report tab displays the Runs (number of tests performed), any Errors (exceptions) found with the test itself, and any test case Failures (incorrect test case assertions).

How to Run Unit Tests in intelliJ

To run unit test from intelliJ, locate the /src/test package as shown below. Right-click on the entire unit test suite, or choose a targeted unit test, then choose the Run '{chosen test}' option.


After running a unit test, the intelliJ event log will display test result data.

How to Run Unit Tests in Gradle

./gradlew test – runs all tests

./gradlew test --tests {*SomeSpecificTestClass}

./gradlew test --tests {*SomeSpecificTestClass.someTestMethod}

./gradlew test --tests {all.in.specific.package*}

For more filtering options, see https://docs.gradle.org/current/userguide/java_plugin.html#test_filtering