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.
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.
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 .
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With