【问题标题】:How to test custom cordova plugin's native code?如何测试自定义cordova插件本机代码?
【发布时间】:2016-08-01 09:54:02
【问题描述】:

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

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

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

【问题讨论】:

  • 找到方法的运气好吗?
  • @EaswaramoorthyK 否。仅使用cordova-plugin-test-framework。
  • 好的。这很好,但无法找到代码覆盖率或将测试结果导出为报告。

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


【解决方案1】:

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

首先,一个activity,别忘了将它添加到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));
}

【讨论】:

  • 如果你想得到回调结果,你应该创建自己的 CordovaWebview 类并覆盖sendPluginResult 方法
【解决方案2】:

就我而言,我不得不稍微修改一下这种方法:

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

然后,创建测试文件(在我的例子中是 MyTestActivityTest.java IN THE 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"
    ...
}

我真的希望这会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多