我正在为android编写单元测试,这个测试应该验证使用的url是否有效,我有一个url不变和一个错误的方法来执行测试,但是测试没有通过。我正在使用JUnit。
public class FeedControleTeste extends ActivityUnitTestCase<TelaFeed> {
private static Context context;
private final String FEED_ADDRESS = "http://noticias.gov.br/noticias/rss?id=AFSZW";
private FeedControle task1, task2;
private TelaFeed tela;
public FeedControleTeste() {
super(TelaFeed.class);
}
public void setUp() throws Exception {
context = getInstrumentation().getTargetContext();
tela = getActivity();
}
public void tearDown() throws Exception {
}
public void testTask() {
final CountDownLatch signal = new CountDownLatch(1);
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
task1 = new FeedControle(context, null);
task2 = new FeedControle(context, tela);
try {
task1.execute(FEED_ADDRESS);
task2.execute("url_errada");
} catch (Exception e) {
fail();
}
}
});
signal.await(30, TimeUnit.SECONDS);
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的错误:
Test failed to run to completion. Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''. Check device logcat for details"
答案 0 :(得分:0)
从这段代码中唯一的东西(假设所有方法,如getTargetContext()确实返回了一些东西)我在下面一行中问自己为什么:
task1 = new FeedControle(context, null);
FeedControle对象初始化为null,而另一次使用ref值?
task2 = new FeedControle(context, tela);
但我不知道那些课程。