Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6038513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:13:57+00:00 2026-05-23T06:13:57+00:00

I have a simple activity with only one edittext which was set to http://

  • 0

I have a simple activity with only one edittext which was set to “http://” in xml. Based on google tutorial, I wrote some unit tests for status check. I want to practice unit test and pass all tests. But I cannot pass testStateDestroy() and testStatePause() (log shows mUrlView=="changed"). The code below is very easy, did I miss something? Thank you in advance.

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}




public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

private Activity mActivity;
private EditText mUrlView;

public MainActivityTest() {
    super("au.com.crystalfish.safeshare.activity", MainActivity.class);
}

protected void setUp() throws Exception {
    super.setUp();
    mActivity = this.getActivity();
    mUrlView = (EditText) mActivity.findViewById(au.com.crystalfish.safeshare.R.id.url);
}

public void testPreconditions() {
    assertNotNull(mActivity);
    assertEquals(mUrlView.getText().toString(), "http://");
}

@UiThreadTest
public void testRotate() {
    mUrlView.setText("changed");
    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    assertEquals(mUrlView.getText().toString(), "changed");
}

@UiThreadTest
public void testStateDestroy() {
    mUrlView.setText("changed");
    assertEquals(mUrlView.getText().toString(), "changed");
    mActivity.finish();
    mActivity = this.getActivity();
    assertEquals(mUrlView.getText().toString(), "http://"); <===========should be "http://" since it is a new activity
}

@UiThreadTest
public void testStatePause() {
    Instrumentation mInstr = this.getInstrumentation();
    mInstr.callActivityOnPause(mActivity);
    mUrlView.setText("changed");
    mInstr.callActivityOnResume(mActivity);
    assertEquals(mUrlView.getText().toString(), "http://");<======should be "http://" since the text should bot be changed when the activity was paused
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:13:58+00:00Added an answer on May 23, 2026 at 6:13 am

    Well, for your testStatePause() test I think that may be valid. Even if the activity is paused you are still free to modify it however you want since you still have a reference to it (or one of its textViews at least. In a test like this, you should change the value of the textView in the actual activity’s onPause method, then use your testStatePause test to verify that onPause was called and did its job correctly, then do the same for onResume().

    I’m not really familiar with Android’s testing framework but maybe a similar issue is happening with your testStateDestroy() test. Your mUrlView could still be pointing to the old, finished Activity. The old activity is no longer valid but its widgets might be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple game that consists of only one activity, and I
Should simple JavaBeans that have only simple getters and setters be unit tested?? What
I have a simple little test app written in Flex 3 (MXML and some
I have a simple application, it starts, loads xml feed from the net, you
I have a pretty simple question which perhaps someone familiar with Server/Client design &
lets say, i have two tables, one for object records and one for activity
I have a script that appends some rows to a table. One of the
I have the following View setup in one of my Activities: <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/photoLayout
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.