如何测试自定义cordova插件的本机代码? [英] How to test custom cordova plugin's native code?
问题描述
我知道 cordova-plugin-test-framework 将有助于测试任何科尔多瓦项目.
我正在为Android平台开发自定义相机Cordova插件.我想为我的自定义相机Android插件代码编写一些Junit/Instrumentation测试用例.
我遇到了问题,因为无法从测试类中创建CordovaInterface
和CordovaWebView
对象.
I know cordova-plugin-test-framework will help in testing any cordova project.
I am developing a custom camera cordova plugin for Android platform. I would like to write some Junit/Instrumentation test cases for my custom camera Android plugin code.
I am facing issues as I can't create CordovaInterface
, CordovaWebView
Objects from my test class.
无论如何,我可以从测试类中创建CordovaInterface
,CordovaWebView
对象,并将它们作为自定义相机Android插件excute
方法的参数传递.
Is there anyway that I can create CordovaInterface
, CordovaWebView
Objects from my test class and pass these as parameters for my custom camera Android plugin excute
method.
我想避免使用cordova-plugin-test-framework在js级别进行单元测试用例,并编写一些Junit测试用例(因为我将可以使用我的相机cordova插件使用的大多数内部类).如果这种错误的方法,请纠正我.
I would like to avoid unit test cases at js level using cordova-plugin-test-framework and write some Junit test cases (as I will have access to most of the internal classes my camera cordova plugin use). please correct me, if this wrong approach.
推荐答案
就我而言,我不得不对此方法进行一些修改:
In my case I had to modify a little this approach:
首先,我的TestActivity
必须与使用<action android:name="android.intent.action.MAIN" />
intent-filter
进行的活动处于同一级别.
First of all, my TestActivity
had to be at the same level as the activity using the <action android:name="android.intent.action.MAIN" />
intent-filter
.
然后,创建测试文件(以我的情况为MyTestActivityTest.java
,在src/androidTest/java/com/your/package/name 中(这对仪器测试非常重要!)
Then, create the test file (in my case MyTestActivityTest.java
IN THE src/androidTest/java/com/your/package/name (super important that this is an Instrumentation Test!!)
然后,在测试本身上:
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
文件具有以下依赖性:
and my build.gradle
file I have this dependencies:
// 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
文件中:
and don't forget to put this also in the build.gradle
file:
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
...
}
我真的希望这会有所帮助
I really hope this helps
这篇关于如何测试自定义cordova插件的本机代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!