gpt4 book ai didi

java - 使用 Robolectric 测试启动 Activity 的代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:21 27 4
gpt4 key购买 nike

我有一些代码想用 Robolectric 测试。基本上,我想测试单击按钮是否会启动 Activity 。

HomeScreenFragment.java:

public class HomeScreenFragment extends Fragment {

private Button mSignInButton;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // call to super class
}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup parent,
Bundle savedInstanceState){

// inflate view
View view = inflater.inflate(R.layout.fragment_home_screen, parent, false);

// handle sign in button
mSignInButton = (Button)view.findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
// anonymous inner class
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SignInActivity.class);// start sign in activity with intent
startActivity(intent); // <<== ERROR HERE WHEN RUNNING TEST

}
}
}
}

我的测试是这样的:HomeSreenFragmentTest.java:

@RunWith(RobolectricTestRunner.class)
public class HomeScreenFragmentTest {

private Activity mHomeScreenActivity;
private Fragment mTestFragment;
private Button mSignInButton;

@Before
public void setup() throws Exception{

mHomeScreenActivity = Robolectric.buildActivity(HomeScreenActivity.class).create().get(); // start HomeScreenActivity, call through to onCreate()
mTestFragment = mHomeScreenActivity.getFragmentManager().findFragmentById(R.id.home_screen_fragment_container);// get HomeScreenFragment

// run onCreateView
View testView = mTestFragment.onCreateView(LayoutInflater.from(mHomeScreenActivity),
(ViewGroup) mHomeScreenActivity.findViewById(R.id.home_screen_fragment_container),
null);

// get button view
mSignInButton = (Button)testView.findViewById(R.id.sign_in_button);

}

// clicking sign in button should launch SignInActivity
@Test
public void testSignInButton2() throws Exception{
mSignInButton.performClick(); <<=== ERROR STARTS HERE
ShadowActivity shadowActivity = Robolectric.shadowOf(mHomeScreenActivity); // create shadow activity
Intent startedIntent = shadowActivity.getNextStartedActivity(); // get intent of next activity on stack
ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent); // create shadow intent which starts next activity
assertEquals(SignInActivity.class.getName(), shadowIntent.getComponent().getClassName()); // compare shadow intent w/ desired next activity
}

我遇到的问题是测试。代码本身在模拟器/设备上运行良好。问题是当 Robolectric 运行 performClick() 方法,然后到达 onClick() 然后转到 startActivity(intent) 它失败了.

堆栈跟踪:

java.lang.NullPointerException: null
at android.app.Activity.startActivityFromFragment(Activity.java:3850)
at android.app.Activity.startActivityFromFragment(Activity.java:3825)
at android.app.Fragment.startActivity(Fragment.java:996)
at android.app.Fragment.startActivity(Fragment.java:975)
at com.********.android.***project*****.controller.HomeScreenFragment$1.onClick(HomeScreenFragment.java:42)
at android.view.View.performClick(View.java:4084)
at com.*********.android.***project***.HomeScreenFragmentTest.testSignInButton2(HomeScreenFragmentTest.java:83)

我知道如何使用 Robolectric.buildActivity() 方法启动 Robolectric Activity 。但这是在我需要测试 Activity 时使用的。为什么 Robolectric 无法在代码中运行 startActivity() 方法?有没有更好的方法来测试这个?

最佳答案

您还应该在 ActivityController 上调用 .start().resume(),而不仅仅是 .create() 这会导致 fragment 也将被创建。

如果您执行上述操作,您将不需要自己调用 onCreateView。您可以使用 mHomeScreenActivity.findViewById(R.id.sign_in_button);

从 Activity 中获取按钮

一般来说,我怀疑你的代码失败是因为 fragment 还没有完全开始,因为你没有调用 start()resume()

关于java - 使用 Robolectric 测试启动 Activity 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19454416/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com