Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create DummyActivity inside androidTest folder for testing

Tags:

I have created a dummy activity inside androidTest folder and declared that activity in AndroidManifest file in androidTest folder.

My basic intention is to test a reusable fragment by putting it into a dummy activity with a framelayout container.

AndroidManifest.xml inside androidTest folder

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     package="com.droid.test"     android:versionCode="1"     android:versionName="1.0">     <uses-sdk         android:minSdkVersion="18"         tools:overrideLibrary="android.support.test.uiautomator.v18" />     <instrumentation         android:name="android.test.InstrumentationTestRunner"         android:targetPackage="com.droid" />      <application>         <uses-library android:name="android.test.runner" />         <activity             android:name="com.droid.DummyActivityForTest"             android:screenOrientation="portrait">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.DEFAULT" />                 <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application> </manifest> 

My test class TestWidgets.java

public class TestWidgets extends ActivityInstrumentationTestCase2<DummyActivityForTest> {     private AppCompatActivity mActivity;      public TestWidgets() {         super(DummyActivityForTest.class);     }      @Override     public void setUp() throws Exception {         super.setUp();         mActivity = getActivity();     }      @Test     public void testAddSpecializationClick() {         onView(withId(R.id.widgets_rv)).perform(                 RecyclerViewActions.actionOnItemAtPosition(4, click()));         Assert.fail("Not Implemented");     } 

When I run my test class its throwing below exception,

java.lang.RuntimeException: Could not launch activity at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:373) at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119) at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97) at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104) at com.practo.droid.home.TestWidgets.setUp(TestWidgets.java:48) at junit.framework.TestCase.runBare(TestCase.java:132) at junit.framework.TestResult$1.protect(TestResult.java:115) at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77) at junit.framework.TestResult.run(TestResult.java:118) at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55) at junit.framework.TestCase.run(TestCase.java:124) at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103) at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69) at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879) Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.practo.droid/.DummyActivityForTest } at android.app.Instrumentation.startActivitySync(Instrumentation.java:385) at android.support.test.runner.MonitoringInstrumentation.access$201(MonitoringInstrumentation.java:90) at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:353) at android.support.test.runner.MonitoringInstrumentation$5.call(MonitoringInstrumentation.java:350) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) 

I don't have much experience in Android testing, somebody please help with some suggestions.

like image 813
Sreedhu Madhu Avatar asked Mar 29 '16 06:03

Sreedhu Madhu


1 Answers

There are 2 APK generated during project building. First is APK with application and second APK contains test. If you put your activity in test folder it will be in second APK that is used for test and your application APK does not contains it. This is why you are receiving this error (because your application APK does not have such activity).

So the only way for you is to put your activity not in test folder but in sources. You can create several application variants (see details here), so when you are building your APK in production your dummy activity will not be included in it.

like image 82
Vyacheslav Pedak Avatar answered Oct 02 '22 00:10

Vyacheslav Pedak