Search code examples
javajunitibm-mobilefirstmobilefirst-adaptersmobilefirst-server

Null Exception when trying to access worklight.properties from junit


Using MobileFirst Platform 6.3.

When I am trying to get the config path of the server folder in worklight project from worklight.properties using junit the value returned is null.

I am using the following code to fetch the path.

WorklightConfiguration.getInstance().getString("mbaas.configRootDir");

Edit: this is what I am trying to do. I am running this code in junit, it should return true.

public class Test2 {
    @Test 
    public void test() { 
        //customProperty=123 
        String str=getWorklightProperty("customProperty"); 
        assertEquals("123", str);
    }

    public String getWorklightProperty(String propertyName) {
        return WorklightConfiguration.getInstance().getString(propertyName);
    }
}

Solution

  • Edit: this will not work for you with JUnit. When you run this code, it is expected to connect to a Worklight Server.

    When you run it by invoking an adapter, it adapter is talking with a server and this is why you can get a response.

    When you run it directly, it has no server to talk with and this is why you get null.


    You need to verify that your code is valid.
    I have tested the following in MFP 6.3 and successfully retrieved the value from worklight.properties:

    1. Added in server/conf/worklight.properties the following property at the very bottom:

      customProperty=123
      
    2. Created a new Java class in server/conf/java:

      package com.myClass.customcode;
      
      import com.worklight.server.bundle.api.WorklightConfiguration;
      
      public class Test {
          public static String getWorklightProperty(String propertyName){
              return WorklightConfiguration.getInstance().getString(propertyName); 
          }
      }
      
    3. Created a new HTTP adapter with the following in the -impl.js file:

      function test() {
          return {
              result: com.myClass.customcode.Test.getWorklightProperty("customProperty")
          }
      }
      

    After invoking the procedure "test", the response was:

        {
           "isSuccessful": true,
           "result": "123"
        }