jeudi 28 juillet 2016

How to Mock/override/invoke CordovaPlugin's execute Method?

I am developing a custom Cordova Plugin in Android. Like any other plugins - My plugin consists of exposed JS api as well as Android classes that gets invoked when My Plugin JS api called from Javascript.

Now I would like to write some Junit or Instrumentation tests for my native Android Plugin code.

Below one is the main class in my plugin that gets invoked from JS with support from cordova

public class MainPlugin extends CordovaPlugin {


    @Override
    public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {

        super.initialize(cordova, webView);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        MyAction1.cleanUp();
        MyAction2.cleanUp();    
    }


    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

        if (action.contains("Action1")) {
             MyAction1.getInstance(cordova).executeAction(action, args, callbackContext);
        } else if (action.contains("Action2") ) {
             MyAction2.getInstance(cordova, webView).executeAction(action, args, callbackContext);
        } else {
            mCallbackContext.error("Action Undefined");
            return false;
        }
        return true;
    }

}

As you see from

public boolean execute(String action, JSONArray args, CallbackContext callbackContext)

method, I am invoking some internal plugin classes which actual do some job and send response.

My plan is to test my internal classes by mocking/invoking MainPlugin's execute method. All my internal classes heavily depend on String action, JSONArray args, CallbackContext callbackContext and calls some third party libraries. i.e I need these parameters actual values or some default values passed from unit test code to test my internal logic.

Please help me with some code/suggestions, best way to test MyPlugin internal classes. Is there any lib that mimic the cordova behavior and able to invoke excutemethod from java test code

Aucun commentaire:

Enregistrer un commentaire