开发者

SingleLaunchActivityTestCase not working when two different classes extend it

This is the scenario:

I am having two classes extended from Single开发者_JAVA技巧LaunchActivityTestCase.

public class ABCTest extends SingleLaunchActivityTestCase<ABCActivity>

and

public class XYZTest extends SingleLaunchActivityTestCase<XYZActivity>

These are their respective constructors and setUp methods:

public ABCTest() {
    super("com.android", ABCActivity.class);
}

@Override
protected void setUp() throws Exception
{
    super.setUp();
    mActivity = this.getActivity();
}

and

public ABCTest() {
    super("com.android", XYZActivity.class);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    mActivity = this.getActivity();
}

Now on running using te android testRunner the testcases in the ABCTest work but for the XYZTest i get the following exception:

java.lang. java.lang.ClassCastException: com.android.ABCActivity
at com.instrument.XYZTest.setUp(XYZTest.java:45)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

But on running these tests , i.e the ABC Tests and the XYZ tests separately it works fine.

The problem seems to be with the line this.getActivity() in the setUp() method. Aftes ABC tests are through the setUp() of XYZ tests returns com.android.ABCActivity for this.getActivity() instead of com.android.XYZActivity

Seems strange........can anyone please help out??????


I've also been having problems with getting the Activity from the previous test as the current Activity class.

I think it is a problem with the SingleLaunchActivityTestCase. If you are using this in your tests, I think your test suite should only be run with one single Activity. If you are testing multiple activities, I recommend using ActivityInstrumentationTestCase2 instead.

You could try to finish the Activity in your teardown methods and set this.activity to null also if you wish to continue using SingleLaunchActivityTestCase and see if that helps:

public void tearDown() {
  super.tearDown();
  this.getActivity().finish();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜