Can't get my assert current activity to work

戏子无情 提交于 2019-12-07 14:10:46

问题


I have 2 activities, the first one is the startup one which in it's create method causes the second one to be launched, always. My Robolectric tests pass fine

Activity

public class LoginActivity extends Activity {

/** Called when the activity is first created. */
@Override    

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loginview);

    Intent intent = new Intent(this,MainActivity.class);
    startActivity( intent );
}

I know my activity works fine cause it launches in my device and on the emulator

My Robolectric tests

public void testLoginFirstTime() throws Exception 
{
    LoginActivity activity = new LoginActivity();        
    activity.onCreate(null);
    assertThat(activity, new StartedMatcher(MainActivity.class));        
}

My Robotium test

public void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}


public void testLoginFirstTime() throws Exception 
{
    solo.assertCurrentActivity("Expected MainActivity to launch", MainActivity.class);
}

What is wrong with my robotium assertion? it always thinks the current activity is login one even though as I watch the emulator/device I can see that Robotium does actually launch the MainActivity but it doesn't appear to know that the new activity has been launched. Edit: Meant to say if I add a button to my login view and launch the new activty via a button click then Robotium performs the click and detects the new activity has been launched ok.

Edit: Looks like its a Robotium limitation http://groups.google.com/group/robotium-developers/browse_thread/thread/79a70038c16e35e6 However it still leaves me with the issue of how to test my app with robotium the same way as a user will use it, ie, not cheating and starting in a different activity :(


回答1:


You need to use the constructor solo = new Solo(Instrumentation instrumentation) and then after you have created the Solo object you call getActivity() in order to start the first Activity. Then it will work.



来源:https://stackoverflow.com/questions/7849938/cant-get-my-assert-current-activity-to-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!