2015-09-23 25 views
2

我正在嘗試使用Robolectric來測試NFC輔助類。簡化的,我在做這樣的事情:使用Robolectric的ShadowNfcAdapter來單元測試NFC輔助類

Activity activity = Robolectric.buildActivity(Activity.class) 
    .create() 
    .start() 
    .resume() 
    .get(); 
NfcAdapter nfcAdapter = ShadowNfcAdapter.getNfcAdapter(activity); 

,然後行使我的NFC輔助類,因爲它確實是這樣的:

if (!nfcAdapter.isEnabled()){ 
    //more stuff 
} 

但是,調用nfcAdapter.isEnabled()導致空指針異常。

java.lang.NullPointerException 
at android.nfc.NfcAdapter.isEnabled(NfcAdapter.java:621) 
at <package>.test_NfcIntent_shouldPostToBus(NfcHelperTest.java:49) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188) 
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:152) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 

例外看起來是從Android的NfcAdapter.java

public boolean isEnabled() { 
    try { 
     return sService.getState() == STATE_ON; 
    } catch (RemoteException e) { 
     attemptDeadServiceRecovery(e); 
     return false; 
    } 
} 

來這裏sService爲空。實際上,當我使用調試器時,我可以看到nfcAdapter的字段都是null或者它們本身具有空字段的對象。

這是正確的使用方法ShadowNfcAdapter?或者,測試NFC輔助類在被觸發時是否做正確的事情的正確方法是什麼?

回答

1

它與Robolectric一個問題,我不只是一個可能的方式立即解決這將是寫自己的影子類。

我創建的Robolectric github上https://github.com/robolectric/robolectric/issues/2059

+0

https://github.com/robolectric/robolectric/pull/2088解決#2059。因此,使用robolectric 3.0或更高版本,它應該工作。 –

2

Coud你試試這樣:

final Activity activty = Robolectric.setupActivity(Activity.class); 
NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activty); 

final ShadowNfcAdapter shadowNfcAdapter = shadowOf(adapter); 

shadowNfcAdapter.isEnabled() 
+1

'ShadowNfcAdapter'沒有'的IsEnabled()'http://robolectric.org/ javadoc/3.0/org/robolectric/shadow/ShadowNfcAdapter.html –

1

與robolectric 3.2.2合作問題

NfcManager manager = (NfcManager) 
context.getSystemService(Context.NFC_SERVICE); 
NfcAdapter nfcAdapter = manager.getDefaultAdapter(); 
Shadows.shadowOf(nfcAdapter).setEnabled(true);