【发布时间】:2015-07-22 17:23:53
【问题描述】:
我正在尝试本地 android 测试(在本地 JVM 上运行的测试,而不是在设备上)。我知道 android.jar 中的类不能正确使用,除非它们被模拟,但由于某种原因,当我尝试实例化一个新视图(或我自己的从视图继承的类)时 - 我得到空值。这完全打破了我的信念,即“new”在 Java 中永远不会返回 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+'
}
谁能提供一些关于这里发生了什么样的魔法的线索?
【问题讨论】:
-
我应该关闭它,否则它可能会为其他没有考虑隐式模拟
toString()方法的人服务?
标签: java android unit-testing null