Unit testing code

This documents applies to version 4.0 P22 and more recent and to modules using Java as server language.

This document implies that you are exposing your Java-implemented modules on the Git (/git) endpoint. When doing so your module is in fact exposed as an Apache Maven projects.

Local unit testing

You can unit test your Java code of your module's Maven project by adding JUnit test classes in the test folder.

For instance:

package com.simplicite.tests;

import static org.junit.Assert.assertEquals;
import org.junit.Test;


/**
 * Local unit test example
 */
public class MyLocalTest {
    @Test
    public void testSomething() {
        assertEquals(/* Something */, /* Some other thing */));
        // Etc.
    }
}

Then you can run the tests by a simple:

mvn test

Note: Of course these tests can use only local features, you can't, for instance, test methods using a grant or a business object. This unit testing is thus limited to code not depending on Simplicité® server components.

Server unit testing

Server unit testing can be aither plain test methods or JUnit shared code classes that you run within the Simplicité® server.

For instance:

package com.simplicite.commons.MyModule;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;

import org.json.JSONObject;

import com.simplicite.util.Grant;
import com.simplicite.util.ObjectDB;
import com.simplicite.util.ObjectField;

/**
 * Server unit test example
 */
public class MyServerTest {
    @Test
    public void testMyActionOfMyObject() {
        try {
            ObjectDB obj = Grant.getSystemAdmin().getTmpObject("MyObject");
            obj.setValues(new JSONObject().put("myCode", "MYCODEVALUE"));
            ObjectField f = prd.getField("myOtherField");
            String res = obj.invokeAction("myAction");
            assertEquals(res, "Something");
            assertEquals(f.getValue(), "Something else");
            // Etc.
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
}

In such test methods/classes you can use any of the server components.

To run such tests you need to implement at least the unitTests methods in your configuration items' classes. This is applicable to:

A Run unit tests action button is available to run the unitTests method (as this is an action the call can be automated using the API or I/O endpoint).

For instance:

@Override
public String unitTests() {
    return new JUnitTool(getGrant()).run("com.simplicite.commons.MyModule.MyServerTest");
}

It is also possible to use a nested static JUnit class:

public static class UnitTest {
    // JUnit test methods as above
}

@Override
public String unitTests() {
    return new JUnitTool(getGrant()).run(UnitTest.class);
}

Run from UI

To invoke the unit tests from the UI, open the definition of your business object, business worflow, external object, ... and click on the Unit tests action button (may be in the "plus" menu).

The result is displayed in a popup.

Run from API

You can also invoke the action from the API endpoint (for instance if you want to automate the tests execution).

Example 1: for a business object

curl -s -u <credentials> <base URL>/api/rest/ObjectInternal/<business object row ID>?_action=runUnitTests | jq -r '.result'

Note: you can get the row ID of your business object from its logical name by:

curl -s -u <credentials> <base URL>/api/rest/ObjectInternal?obo_name=<business object logical name> | jq -r '.[].row_id'

Example 2: for a business workflow

curl -s -u <credentials> <base URL>/api/rest/BPMProcess/<business workflow row ID>?_action=runUnitTests | jq -r '.result'

Note: you can get the row ID of your business workflow from its name by:

curl -s -u <credentials> <base URL>/api/rest/BPMProcess?pcs_name=<business workflow name> | jq -r '.[].row_id'

Example 3: for a external object

curl -s -u <credentials> <base URL>/api/rest/ObjectExternal/<external object row ID>?_action=runUnitTests | jq -r '.result'

Note: you can get the row ID of your external object from its name by:

curl -s -u <credentials> <base URL>/api/rest/ObjectExternal?obe_name=<external object name> | jq -r '.[].row_id'

Etc.

Run from I/O

As of version 4.0 P23 you can also invoke the action from the I/O services endpoint (for instance if you want to automate the tests execution).

Example:

curl -s -u <credentials> --form service=unittests --form <type = object or extobject or process etc.>=<name> <base URL>/io

Note: if you are using the instances manager the above command can be issued as:

sim ant-unittests <instance name>

The type and the name are prompted.