Junit使用ActivityInstrumentationTestCase2 - 构造函数中的异常:Stub

时间:2012-09-27 02:37:39

标签: android intellij-idea junit4

我刚刚开始在Android测试中使用JUNIT。我目前正在尝试使用它来测试DAO对象。我正在使用intellij作为IDE。

我已经收集到了,我应该使用扩展ActivityInstrumentationTestCase2的测试类,并使用那里的检测工具来获取启动DAO对象所需的上下文。

这是我的测试类:

package br.com.pcontop.vigilantes.model;

import android.content.*;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.test.ActivityInstrumentationTestCase2;
import br.com.pcontop.vigilantes.view.PaginaDia;
import junit.framework.TestCase;

import java.io.*;

public class EntradaPontosDAOTest extends ActivityInstrumentationTestCase2<PaginaDia> {
    Context context;

    public EntradaPontosDAOTest() {
        super("br.com.pcontop.vigilantes.view", PaginaDia.class);

    }

    public void setUp() throws Exception {
        super.setUp();
        context = getInstrumentation().getContext();
    }
    public void testBusqueEntradasComecandoCom() throws Exception {
        //assertNotNull(context);
        EntradaPontosDAO entradaPontosDAO = new EntradaPontosDAO(context);
        //assertNotNull(entradaPontosDAO);

    }
}

当我运行测试时,抛出以下异常:

junit.framework.AssertionFailedError: Exception in constructor: testBusqueEntradasComecandoCom (java.lang.RuntimeException: Stub!
    at android.test.InstrumentationTestCase.<init>(InstrumentationTestCase.java:5)
    at android.test.ActivityTestCase.<init>(ActivityTestCase.java:5)
    at android.test.ActivityInstrumentationTestCase2.<init>(ActivityInstrumentationTestCase2.java:5)
    at br.com.pcontop.vigilantes.model.EntradaPontosDAOTest.<init>(EntradaPontosDAOTest.java:33)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at junit.framework.TestSuite.createTest(TestSuite.java:61)
    at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
    at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
    at junit.framework.TestSuite.<init>(TestSuite.java:129)
    at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:71)
    at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
    at com.intellij.junit4.JUnit46ClassesRequestBuilder.collectWrappedRunners(JUnit46ClassesRequestBuilder.java:69)
    at com.intellij.junit4.JUnit46ClassesRequestBuilder.getClassesRequest(JUnit46ClassesRequestBuilder.java:40)
    at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:81)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
)

有问题的一行是这个,在构造函数中:

    super("br.com.pcontop.vigilantes.view", PaginaDia.class);

我已尝试将junit jar放在项目类路径中的其他jar上。它现在适用于扩展TestCase的测试。对于上面的测试,这仍然没有做到。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

我落在了link上,希望它会有所帮助。您需要在构建路径设置中将junit向上移动依赖链。

答案 1 :(得分:0)

您应该在不在开发计算机上的Android设备或模拟器中运行Android测试用例。 SDK上的android.jar仅提供存根。