如何测试自定义cordova插件的本机代码?

时间:2016-08-01 09:54:03

标签: android cordova unit-testing junit cordova-plugins

我知道cordova-plugin-test-framework将有助于测试任何cordova项目。 我正在为Android平台开发一个自定义相机cordova插件。我想为我的自定义相机Android插件代码编写一些Junit / Instrumentation测试用例。 我遇到了问题,因为我无法从我的测试类中创建CordovaInterfaceCordovaWebView个对象。

无论如何,我可以从我的测试类中创建CordovaInterfaceCordovaWebView个对象,并将这些对象作为自定义相机Android插件excute方法的参数传递。

我想使用cordova-plugin-test-framework来避免js级别的单元测试用例并编写一些Junit测试用例(因为我可以访问我的相机cordova插件使用的大多数内部类)。如果这是错误的做法,请纠正我。

2 个答案:

答案 0 :(得分:1)

首先,将CordovaLib作为库复制到您的插件项目中。 将这样的内容添加到Test类中:

首先,一个活动,不要忘记将其添加到AndroidMainfest.xml进行测试。

public class TestActivity extends CordovaActivity {

    CordovaWebView getWebView(){
        return this.appView;
    }
    CordovaInterface getInterface(){
        return this.cordovaInterface;
    }

    public void init(){
        super.init();
    }
}

在您的测试课程中:

@Rule
public ActivityTestRule<CordovaActivity> mActivityRule =
    new ActivityTestRule<CordovaActivity>(CordovaActivity.class);



private CordovaWebView wv;
private CordovaInterface cordovaInterface;
private YourCordovaPlugin km;
private TestActivity activity;

@Before
public void Init() throws InterruptedException {
    km = new KM1930CordovaPlugin();
    activity = mActivityRule.getActivity();

    Runnable action = new Runnable() {
        @Override
        public void run() {
            activity.init();
            wv =  mActivityRule.getActivity().getWebView();
            cordovaInterface = mActivityRule.getActivity().getInterface();
            km = new YourCordovaPlugin();
            km.initialize(cordovaInterface,wv);
            wv.getPluginManager().addService(new  PluginEntry("YourServiceName",km));
            synchronized (this) {
                this.notify();
            }
        }
    };
    synchronized( action ) {
        activity.runOnUiThread(action);
        action.wait() ;
    }
}

然后,您可以通过调用execute方法添加测试方法和调用方法。

@Test
public void TestBluetooth() throws JSONException {
        this.km.execute("echo","[]",new CallbackContext("0",wv));
}

答案 1 :(得分:1)

就我而言,我不得不对此方法进行一些修改:

首先,我的TestActivity必须与使用<action android:name="android.intent.action.MAIN" /> intent-filter进行的活动处于同一级别。

然后,创建测试文件(在我的情况下为MyTestActivityTest.java 在src / androidTest / java / com / your / package / name 中(这对仪器测试非常重要! !)

然后,在测试本身上:

import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;

@RunWith(AndroidJUnit4.class)
public class MyTestActivityTest {

    @Rule
    public ActivityTestRule<TestActivity> mActivityRule = new ActivityTestRule<>(TestActivity.class);

    @Rule
    public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();

    private YourPlugin mockYourPlugin;

    private CordovaWebView cordovaWebView;
    private CordovaInterface cordovaInterface;
    private TestActivity activity;

    @Before
    public void setUp() throws Throwable {

        activity = mActivityRule.getActivity();
        assertNotNull(activity);

        final CountDownLatch signal = new CountDownLatch(1); --> this one is the important! the sync part got stucked!

        Runnable action = () -> {
            activity.init();
            cordovaWebView = mActivityRule.getActivity().getWebView();
            cordovaInterface = mActivityRule.getActivity().getInterface();
            mockYourPlugin = new YourPlugin();
            mockYourPlugin.initialize(cordovaInterface, cordovaWebView);
            cordovaWebView.getPluginManager().addService(new PluginEntry("YourServiceName", mockBankId));

            signal.countDown();// notify the count down latch, i.e release it
        };

        uiThreadTestRule.runOnUiThread(action);

        signal.await();// wait for callback
    }

    @Test
    public void TestBluetooth() throws JSONException {
        this.mockYourPlugin.execute("echo","[]",new CallbackContext("0", cordovaWebView));
    }

}

和我的build.gradle文件具有以下依赖性:

// JUnit 4 framework
implementation 'junit:junit:4.12'
// required if you want to use Mockito for unit tests
implementation 'org.mockito:mockito-core:2.13.0'
implementation 'org.mockito:mockito-android:2.8.47'

implementation 'com.android.support.test:rules:1.0.2'

不要忘记将其也放置在build.gradle文件中:

defaultConfig {
    ...
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    ...
}

我真的希望这会有所帮助