2016-05-15 57 views
2

我使用Android Studio 2.0,并且我是Android的新手。 在我的源代码中,Xml.newPullParser()很好用 但是,当我在junit test(测试目录)中输入它时,我发现它返回null。为什么会发生?我对它感到困惑。为什么Xml.newPullParser()在Android测试中返回null?

第二个问题,有两个目录(test和androidTest),它们之间有什么不同?我应该使用哪一个?

package com.ecust.ecusthelper.util.network.httpurlconnection; 

import android.util.Xml; 

import org.junit.Assert; 
import org.junit.Test; 
import org.xmlpull.v1.XmlPullParser; 

public class XmlTest { 

    @Test 
    public void testXmlNonNull() { 
     XmlPullParser pull = Xml.newPullParser(); 
     if (pull == null) 
      Assert.fail("Why here is null"); 
    } 
} 

我的build.gradle

apply plugin: 'com.android.application' 
apply plugin: 'me.tatarka.retrolambda' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId "com.ecust.ecusthelper" 
     minSdkVersion 15 
     targetSdkVersion 23 
     versionCode 2 
     versionName '1.1' 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    productFlavors { 
    } 
    compileOptions { 
     sourceCompatibility JavaVersion.VERSION_1_8 
     targetCompatibility JavaVersion.VERSION_1_8 
    } 
    testOptions { 
     unitTests.returnDefaultValues = true 
    } 
} 

dependencies { 
    testCompile 'junit:junit:4.12' 
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2' 
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2' 
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2' 
    compile 'com.android.support:appcompat-v7:23.2.1' 
    .... 
} 
+0

你可以sh是'build.gradle'的'junit'配置吗?你确定你指定了一个正确的'testInstrumentationRunner'吗? – x0r

+0

ohh,我不知道如何使用'testInstrumentationRunner'。这是我的'build.gradle',我使用'unitTests.returnDefaultValues = true' – chenjj2048

回答

0

我认为你需要在你的build.gradledefaultConfig部分指定测试运行:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

所以它看起来像这样:

defaultConfig { 
    applicationId "com.ecust.ecusthelper" 
    minSdkVersion 15 
    targetSdkVersion 23 
    versionCode 2 
    versionName '1.1' 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
}