6

我刚刚开始在 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 的测试中运行良好。这仍然不能解决上面的测试。

我究竟做错了什么?

4

2 回答 2

1

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

于 2013-01-25T09:26:56.950 回答
0

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

于 2012-09-27T03:04:14.203 回答