Reputation: 1444
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.
Upvotes: 19
Views: 7101
Reputation: 41126
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:
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>
Upvotes: 9
Reputation: 11230
The test project and activity can coexist together, put the target package name as the test project's package name
Upvotes: 0