2016-02-25 96 views
0

我試圖讓XmlPullParser工作,因爲它比替代品更有意義。然而,由於XmlPullParserFactory.newInstance()函數返回一個空工廠(由調試器看到),我正在陷入第一個障礙。因此,只要調用factory.newPullParser()就會引發異常。Android:XmlPullParserFactory.newInstance()創建一個空工廠

import java.io.IOException; 
import java.io.InputStream; 
import java.io.StringReader; 

import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 
import org.xmlpull.v1.XmlPullParserFactory; 

public class XMLParser 
{ 
    public static void getXML(InputStream in) 
      throws XmlPullParserException, IOException { 
     try { 
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
      XmlPullParser xpp = factory.newPullParser(); 

      xpp.setInput(new StringReader("<foo>Hello World!</foo>")); 
      int eventType = xpp.getEventType(); 
      while (eventType != XmlPullParser.END_DOCUMENT) { 
       if (eventType == XmlPullParser.START_DOCUMENT) { 
        System.out.println("Start document"); 
       } else if (eventType == XmlPullParser.START_TAG) { 
        System.out.println("Start tag " + xpp.getName()); 
       } else if (eventType == XmlPullParser.END_TAG) { 
        System.out.println("End tag " + xpp.getName()); 
       } else if (eventType == XmlPullParser.TEXT) { 
        System.out.println("Text " + xpp.getText()); 
       } 
       eventType = xpp.next(); 
      } 
      System.out.println("End document"); 
     } catch (Exception e) { 
     } 
    } 
} 

我從單元測試中調用此方法。我真的難以理解這個問題,因爲我還沒有發現任何有此類問題的帖子。

跟蹤報告: 全部線程轉儲

"[email protected]" prio=5 tid=0x1 nid=NA runnable 
    java.lang.Thread.State: RUNNABLE 
    at com.company.app.XMLParser.getXmln(XMLParser.java:40) 
    at com.company.app.XMLParserTest.setUp(XMLParserTest.java:36) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    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.RunBefores.evaluate(RunBefores.java:24) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    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) 

"[email protected]" daemon prio=8 tid=0x3 nid=NA waiting 
    java.lang.Thread.State: WAITING 
    at java.lang.Object.wait(Object.java:-1) 
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) 
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164) 
    at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209) 

"Reference [email protected]" daemon prio=10 tid=0x2 nid=NA waiting 
    java.lang.Thread.State: WAITING 
    at java.lang.Object.wait(Object.java:-1) 
    at java.lang.Object.wait(Object.java:502) 
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157) 

"Attach [email protected]" daemon prio=5 tid=0x5 nid=NA runnable 
    java.lang.Thread.State: RUNNABLE 

"Signal [email protected]" daemon prio=9 tid=0x4 nid=NA runnable 
    java.lang.Thread.State: RUNNABLE 
+0

它只是返回一個新創建的實例;沒有機會在那裏變空。請將Log.wtf(「PullParser」,e)'添加到您的catch子句併發布結果。 – tynn

+0

報告的異常是NullPointerException。 –

+0

爲我的問題添加了跟蹤報告。希望它是你的後代。 –

回答

0

你不能Android的單元測試組件。用於編譯的JAR文件僅包含引發異常的方法存根。考慮一個簡單的測試案例爲您的問題

public class UnitTest { 
    @Test 
    public void instanceNotNull() throws Exception { 
     assertNotNull(XmlPullParserFactory.newInstance()); 
    } 
} 

沒有定義的任何配置下運行它,你會得到一個

java.lang.RuntimeException: Method newInstance in org.xmlpull.v1.XmlPullParserFactory not mocked. See http://g.co/androidstudio/not-mocked for details. 
    at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java) 
    at ... 

當添加

testOptions { 
    unitTests.returnDefaultValues = true 
} 

你的android的gradle配置,會發現測試在斷言上失敗。

如果您想使用android庫進行測試,您需要在具有儀器測試的設備上進行測試。所以,把你的測試用例在androidTest源中,加

dependencies { 
    testCompile 'junit:junit:4.12' 
    androidTestCompile 'com.android.support.test:runner:0.4' 
} 

您模塊的文件的gradle和設備上運行它。

欲瞭解更多信息,請參閱Testing ConceptsTesting Support Library

相關問題