Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define Activity inside Android test project and run a test against it?

Tags:

android

junit

I'm writing tests for a component in my Android application. This component uses activities to make some reports. So I need an activity in order to test the component (ugly architecture) and I thought it would be easy to create a dummy activity inside the test project and than create tests inherited from ActivityInstrumentationTestCase2<TestActivity>, but for some reason I always get java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.xxx/.Testctivity } exception.

Test activity is added into the manifest file and package seems to be set correctly.

I've tried to put it in the both com.xxx (package of the application) and com.xxx.test packages, with no luck. But when I move TestActivity into the target application everything works fine. So I started wondering what is the difference between the test project and my application and is it even possible to have activities inside the test projects.

like image 408
Fedor Avatar asked Mar 01 '12 15:03

Fedor


People also ask

Why do you use the Android Junitrunner when running UL tests?

When you use AndroidJUnitRunner to run your tests, you can access the context for the app under test by calling the static ApplicationProvider. getApplicationContext() method. If you've created a custom subclass of Application in your app, this method returns your custom subclass's context.

How do you get activity from Activityscenariorule?

You can access the ActivityScenario instance via getScenario() . You may finish your activity manually in your test, it will not cause any problems and this rule does nothing after the test in such cases. This rule is an upgraded version of the now deprecated ActivityTestRule .


1 Answers

Yes, it is possible but not recommended, as it stated in the official dev guide:

Once you have created a test project, you populate it with a test package. This package does not require an Activity, although you can define one if you wish. Although your test package can combine Activity classes, test case classes, or ordinary classes, your main test case should extend one of the Android test case classes or JUnit classes, because these provide the best testing features.

In order to do this, you need:

  1. Define your dummy activity in Test Project's AndroidManifest.xml.
  2. Change the instrumentation targetPackage point to itself in Test Project's AndroidManifest.xml. (the Activity class under test must under the instrumentation targetPackage).

Suppose I have a Test Project com.example.test contain two class DummyActivity and DummyActivityTest, then if you want test DummyActivity using DummyActivityTest, you need to define your Test Project's AndroidManifest.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<!-- targetPackage point to test project itself -->
<instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.example.test" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <uses-library android:name="android.test.runner" />
    <activity
        android:name=".DummyActivity"
        android:label="@string/app_name" >
    </activity>
</application>

like image 137
yorkw Avatar answered Oct 01 '22 13:10

yorkw