Junit using ActivityInstrumentationTestCase2 - Conception Exception: Stub

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

I compiled that I am using a test class that extends ActivityInstrumentationTestCase2, and I use the hardware there to get the context needed to initiate a 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); } } 

When 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 corresponding line is as follows:

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

I tried alredy to put junit jar on top of the rest in the project classpath. Now it works well in tests extending TestCase. This still does not do the test trick above.

What am I doing wrong?

+6
source share
2 answers

You should run Android test files on an Android device or emulator, and not on your development computer. android.jar found on your SDK contains only stubs.

0
source

Source: https://habr.com/ru/post/926362/


All Articles