2015-06-29 59 views
4

我在單元測試中使用了robolectric。最近在我們的項目中,我們從zendesk添加了一個新的依賴項。Android Instrumentation使用AndroidJunitRunner和AndroidJUnit4測試java.lang.UnsatisfiedLinkError

repositories { 

    maven { 
     url 'https://zendesk.artifactoryonline.com/zendesk/repo' 
    } 
} 

compile group: 'com.zendesk', name: 'sdk', version: '1.3.0.1' 

現在我們甚至都沒有提及從這個庫中的任何類,我們只是通過增加這種依賴讓我們robolectric單元測試例外。我認爲這個問題出現在我們的項目中,但它也出現在一個樣本robolectric測試項目上。

java.lang.VerifyError: Expecting a stackmap frame at branch target 31 
Exception Details: 
    Location: 
    com/zendesk/sdk/power/BatteryStateBroadcastReceiver.onReceive(Landroid/content/Context;Landroid/content/Intent;) 
V @13: ifnonnull 
    Reason: 
    Expected stackmap frame at this location. 
    Bytecode: 
    0x0000000: b200 1212 0703 bd00 0fb8 0015 2cc7 0012 
    0x0000010: b200 1212 0303 bd00 0fb8 0017 a700 66b2 
    0x0000020: 0012 bb00 1159 b700 1c12 04b6 001d 2cb6 
    0x0000030: 0014 b600 1db6 001e 03bd 000f b800 1512 
    0x0000040: 062c b600 14b6 001b 9900 1ab2 0012 1202 
    0x0000050: 03bd 000f b800 162b b800 1804 b600 19a7 
    0x0000060: 0023 1205 2cb6 0014 b600 1b99 0017 b200 
    0x0000070: 1212 0103 bd00 0fb8 0016 2bb8 0018 03b6 
    0x0000080: 0019 b200 1212 0803 bd00 0fb8 0015 b1 

at java.lang.Class.forName0(Native Method) 
at java.lang.Class.forName(Class.java:264) 
at org.robolectric.internal.Shadow.newInstanceOf(Shadow.java:15) 
at org.robolectric.shadows.ShadowApplication.registerBroadcastReceivers(ShadowApplication.java:148) 
at org.robolectric.shadows.ShadowApplication.bind(ShadowApplication.java:137) 
at org.robolectric.shadows.CoreShadowsAdapter.bind(CoreShadowsAdapter.java:99) 
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:121) 
at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:421) 
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 
+0

您可以檢查最終的AndroidManifest嗎?我假設圖書館爲它增加了一些廣播接收器。但現在我不知道如何解決它 –

回答

6

找到了解決方法。我們在測試用例中沒有使用zendesk代碼。來自增加的依賴關係(zendesk)的廣播接收者通過gradle構建被添加到AndroidManifest.xml中。 RobolectricTestRunner類將清單解析爲其內部的ApplicationManifest對象,然後通過ShadowApplication類設置應用程序狀態。 ShadowApplication類從應用程序清單註冊廣播接收器。這是我得到上述錯誤的地方。

修復: 我們有一個自定義測試運行器,它擴展了RobolectricGradleTestRunner。 因爲我已經覆蓋了getAppManifest方法並刪除了不需要的廣播接收器。我知道它是一種解決方法,但沒有其他選擇。不想創建另一個清單並創建重複。這是代碼片段。

@Override 
protected AndroidManifest getAppManifest(org.robolectric.annotation.Config config) { 
    AndroidManifest manifest = super.getAppManifest(config); 
    List<BroadcastReceiverData> broadcastReceivers = manifest.getBroadcastReceivers(); 
    List<BroadcastReceiverData> removeList = new ArrayList<>(); 
    for(BroadcastReceiverData receiverData : broadcastReceivers) { 
     if(isDeletePackage(receiverData.getClassName())) { 
      removeList.add(receiverData); 
     } 
    } 
    broadcastReceivers.removeAll(removeList); 
    return manifest; 
} 

private boolean isDeletePackage(String className) { 
    for(String s : DELETE_BROADCAST_PACKAGE) { 
     if(className.startsWith(s)) { 
      return true; 
     } 
    } 
    return false; 
} 

DELETE_BROADCAST_PACKAGE只是一個String hashset。包含軟件包層級名稱

相關問題