Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit using ActivityInstrumentationTestCase2 - Exception in constructor: Stub

I'm just starting to use JUNIT with Android testing. I'm currently attempting to use it to test a DAO object. I'm using intellij as the IDE.

I've gathered that I shoudl use a test class extending ActivityInstrumentationTestCase2, and use the instrumentation facilities there to get the context needed to initiate the DAO object.

This is my test class:

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);

    }
}

As I run the test, the following exception is thrown:

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)
)

The line in question is this, in the contructor:

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

I've tried alredy placing the junit jar over the others in the project's classpath. It now works well in tests extending TestCase. This still don't do the trick for the test above.

What am I doing wrong?

like image 358
user1701989 Avatar asked Sep 27 '12 02:09

user1701989


1 Answers

I fell upon this link, hopefully it will be helpful. You need to move junit up the dependency chain in your build path settings.

like image 183
Siddharth Avatar answered Oct 05 '22 22:10

Siddharth