0

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);
    }
}
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
cherry
  • 77
  • 10
  • what version of worklight are you using? – Yoel Nunez Jan 13 '15 at 14:37
  • What is this "config" path you are referring to? There is no such folder, file or property anywhere under the server folder in a Worklight project; please be more precise in your question. Detail the scenario you are trying to achieve. – Idan Adar Jan 13 '15 at 14:46
  • WorklightConfiguration.getInstance().getString("mbaas.configRootDir"); Is how it exactly looks ,And this is defined in the work light.properties file under server/conf folder . – cherry Jan 13 '15 at 17:28
  • mbaas.configRootDir=../worklightproject/server/conf this how mbaas.configRootDir looks like in work light.properties – cherry Jan 13 '15 at 17:34
  • @RahulChadalawada, Thanks for the clarifications. What is the answer to Yoel's question? What is your Worklight version? 6.0/6.1/6.2/6.3? – Idan Adar Jan 13 '15 at 17:45
  • @RahulChadalawada, can you provide your worklight.properties? – Idan Adar Jan 13 '15 at 17:56
  • Sorry Yoel ,I missed you question i am using Worklight version : 6.3 – cherry Jan 13 '15 at 18:16
  • idan Please do look into this [link](https://gist.github.com/sunnystory/4cb28f6cbcd0f9e9db47) for my worklight.properties file – cherry Jan 13 '15 at 18:36
  • Provide your implementation. This may be junit related. – Idan Adar Jan 13 '15 at 19:55

2 Answers2

0

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"
    }
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • The thing is Idan 1)as i mentioned you i am working it through junit so my requirement is to run it through junit .2)It works fine through the adapter but when i say run through junit it throws me null. – cherry Jan 14 '15 at 04:49
  • I have edited the code example you have given as per my requirment in my question.Please do check thank you – cherry Jan 14 '15 at 04:51
  • Any Idea Idan why i am getting the null exception only when running it through junit.Please help. – cherry Jan 14 '15 at 05:12
  • Hi Idan ,Is there any other way i can get the worklight.properties values from junit. – cherry Jan 15 '15 at 08:30
0

Your are trying to Unit Test some code that is normally run in the Worklight Server environment, it depends on

WorklightConfiguration.getInstance().getString(propertyName); 

and that can only function inside the server not when run as a stand-alone Unit test under something such as JUnit.

How to solve this? First, exactly what are you trying to test? Are you really trying to test that WorklightConfiguration.getInstance().getString() works? Why would you do such a thing? Do you propose to test every Worklight API? I claim that your should be Unit testing your code, not Worklight. So if you have code such as this pseudo code:

  figure out the name of a property
  WorklightConfiguration.getInstance().getString(thePropertyWeJustFigured)
  do some stuff with the value we obtained

then you can unit test your code by providing a Mock implementation of the WorklightConfiguration API. You can use frameworks such as JMock, you are then in the position that your code will execute inside JUnit without depedencies on Worklight. This is true UNIT testing, testing without dependedencies.

Personally I don't much favour this approach, the effort in preparing the Mocks is quite large. Instead I prefer to do structured Integration Tests. That is I test the adapter as a whole, while it runs inside the Worklight server. I may well still use JUnit but the tests it runs use an HTTP invocation framework to invoke the adapter. So the test script goes:

 ensure worklight server is running and adapter under test is deployed
 run JUnit tests that issue HTTP requests to the adapter and inspect the results.
djna
  • 54,992
  • 14
  • 74
  • 117