Android 本地单元测试 - 新视图(null) returns null
Android local unit test - new View(null) returns null
我正在试验本地 android 测试(那些 运行 在本地 JVM 上,而不是在设备上)。我知道 android.jar 中的 classes 除非被模拟,否则无法正确使用,但出于某种原因,当我尝试实例化一个新视图(或我自己的 class 继承的从视图中)- 我得到空值。这完全打破了我的信念,即 "new" 在 Java 中永远不会 returns null。
这是我的测试代码 (src/test/java/com/example/FooTest.java
):
package com.example;
import android.view.View;
import org.junit.Test;
public class FooTest {
@Test
public void testView() {
View v = new View(null);
System.out.println("view = " + v);
}
}
此代码在测试报告输出中打印视图为空。
这是我的 build.gradle(项目的其余部分只是由 android create project
生成,没有兴趣):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3+'
}
}
apply plugin: 'android'
android {
compileSdkVersion 'android-22'
buildToolsVersion '23.0.0 rc2'
buildTypes {
release {
minifyEnabled false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
repositories {
jcenter();
}
dependencies {
testCompile 'junit:junit:4.12+'
}
谁能提供一些线索,说明这里发生了什么样的魔法?
此代码仅表明字符串表示形式(即参见 View
的 toString()
)是 "null" 而不是 v
是 null
。
据我所知,new Something()
从不 null
在 java
我正在试验本地 android 测试(那些 运行 在本地 JVM 上,而不是在设备上)。我知道 android.jar 中的 classes 除非被模拟,否则无法正确使用,但出于某种原因,当我尝试实例化一个新视图(或我自己的 class 继承的从视图中)- 我得到空值。这完全打破了我的信念,即 "new" 在 Java 中永远不会 returns null。
这是我的测试代码 (src/test/java/com/example/FooTest.java
):
package com.example;
import android.view.View;
import org.junit.Test;
public class FooTest {
@Test
public void testView() {
View v = new View(null);
System.out.println("view = " + v);
}
}
此代码在测试报告输出中打印视图为空。
这是我的 build.gradle(项目的其余部分只是由 android create project
生成,没有兴趣):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3+'
}
}
apply plugin: 'android'
android {
compileSdkVersion 'android-22'
buildToolsVersion '23.0.0 rc2'
buildTypes {
release {
minifyEnabled false
proguardFile getDefaultProguardFile('proguard-android.txt')
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
repositories {
jcenter();
}
dependencies {
testCompile 'junit:junit:4.12+'
}
谁能提供一些线索,说明这里发生了什么样的魔法?
此代码仅表明字符串表示形式(即参见 View
的 toString()
)是 "null" 而不是 v
是 null
。
据我所知,new Something()
从不 null
在 java