gpt4 book ai didi

android - 使用 Robolectric 3.0 测试 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:12 28 4
gpt4 key购买 nike

我有两个 fragment 附加到一个 Activity 。

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

LoginFragment loginFragment = new LoginFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.add(android.R.id.content,loginFragment);
fragmentTransaction.commit();
}

public void userReg(View view){
RegisterFragment regFragment = new RegisterFragment();
FragmentManager fragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 =
fragmentManager1.beginTransaction();
fragmentTransaction1.addToBackStack("added");
fragmentTransaction1.replace(android.R.id.content,regFragment);
fragmentTransaction1.commit();
}

}

通过按钮附加的注册 fragment 。现在我想在 robolectric 3.0 中测试它们。

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 21)

public class MainActivityTest {
MainActivity activity;
@Before
public void setUp(){
activity= Robolectric.setupActivity(MainActivity.class);
}

@Test
public void shouldNotBeNull(){
assertNotNull(activity);
LoginFragment loginFragment = new LoginFragment();
startFragment(loginFragment);
assertNotNull(loginFragment);
}

@Test
public void loginFragmentShouldNotBeNull(){
RegisterFragment registerFragment = new RegisterFragment();
startFragment(registerFragment);
assertNotNull(registerFragment);

}
@Test
public void registerFragmentShouldNotBeNull(){
RegisterFragment registerFragment = new RegisterFragment();
startFragment(registerFragment);
assertNotNull(registerFragment);

}
}

这些线路提示

        startFragment(loginFragment);
startFragment(registerFragment);

说startFragment(android.app.Fragment)不能分别应用于LoginFragment和Register fragment。

对于我导入的两个 fragment

import android.support.v4.app.Fragment;

所以这是导致问题的原因。我本可以通过使用固定 fragment 来解决这个问题,但是使用动态 fragment 怎么样?

有什么想法吗?

谢谢。

编辑

上述错误已修复。我刚刚添加了

import static org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;

但是,当我运行测试时,出现了另一个异常。

java.lang.ClassCastException: org.robolectric.shadows.support.v4.SupportFragmentTestUtil$FragmentUtilActivity cannot be cast to android.support.v7.app.AppCompatActivity

at fragments.LoginFragment.onCreateView(LoginFragment.java:64)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:144)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:105)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:293)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:143)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:126)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1557)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:654)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)
at org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment(SupportFragmentTestUtil.java:17)
at team.football.ael.MainActivityTest.loginFragmentShouldNotBeNull(MainActivityTest.java:34)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:527)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:265)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:191)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:56)
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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:157)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)



java.lang.ClassCastException:

org.robolectric.shadows.support.v4.
SupportFragmentTestUtil$FragmentUtilActivity
cannot be cast to android.support.v7.app.AppCompatActivity

at fragments.RegisterFragment.onCreateView(RegisterFragment.java:54)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:144)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:30)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:105)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:293)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:143)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:126)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.enqueueMessage(Handler.java:631)
at android.os.Handler.sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageDelayed(Handler.java:570)
at android.os.Handler.post(Handler.java:326)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1557)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:654)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)
at org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment(SupportFragmentTestUtil.java:17)
at team.football.ael.MainActivityTest.registerFragmentShouldNotBeNull(MainActivityTest.java:41)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:527)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:265)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:191)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:56)
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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:157)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

编辑 2

这是我的RegisterFragment

public class RegisterFragment extends Fragment {
private SessionManager session;
EditText etEmail, etUsername, etPassword,etConfirmPassword;
String email, userName, userPass,confirmPassword;
Button registerButton;

public RegisterFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_register, container, false);
Toolbar myToolbar = (Toolbar) v.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(myToolbar);
setHasOptionsMenu(true);


etEmail = (EditText)v.findViewById(R.id.name);
etUsername = (EditText)v.findViewById(R.id.user_name);
etPassword = (EditText)v.findViewById(R.id.user_pass);
etConfirmPassword = (EditText)v.findViewById(R.id.confirm_pass);
registerButton = (Button)v.findViewById(R.id.registerBtn);
// Session manager
// Session manager
session = new SessionManager(getActivity());

if (session.isLoggedIn()) {
// User ites already logged in. Take him to main activity
Intent intent = new Intent(getActivity(), Welcome.class);
startActivity(intent);

}
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
email = etEmail.getText().toString();
userName = etUsername.getText().toString();
userPass = etPassword.getText().toString();
confirmPassword = etConfirmPassword.getText().toString();

if(userPass.equals(confirmPassword)){

registerUser(email, userName, userPass, confirmPassword);

}else{

Toast.makeText(getActivity(),"Passwords don't match",Toast.LENGTH_SHORT).show();
}
}
});

return v;
}
private void registerUser(final String email, final String userName,
final String password,final String confirmPassword) {
// Tag used to cancel the request

//HttpsTrustManager.sssMethod();
String tag_string_req = "req_register";


StringRequest strReq = new StringRequest(Request.Method.POST,
Config.URL_REGISTER, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d("Response", "Register Response: " + response.toString());

try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString("result").equals("success")) {



Toast.makeText(getActivity(),jsonObject.getString("message"), Toast.LENGTH_LONG).show();

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = settings.edit();
editor.putString("id","");
editor.putString("email", email);
editor.putString("user_name", userName);
editor.putString("user_pass", password);
editor.putString("confirm_pass", confirmPassword);


}

else if (jsonObject.getString("result").equals("fail")) {

Toast.makeText(getActivity(),jsonObject.getString("message"), Toast.LENGTH_LONG).show();

}

}catch(JSONException e){
e.printStackTrace();
}


}


}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Registration Error: " + error.getMessage());
Toast.makeText(getActivity(),
error.getMessage(), Toast.LENGTH_LONG).show();

}
}) {

@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("id","");
params.put("email", email);
params.put("user_name", userName);
params.put("user_pass", password);
params.put("confirm_pass", confirmPassword);
return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

最佳答案

您的 fragment 继承自“支持 fragment ”(来自 android 支持库的 fragment ),但您的 startFragment() 函数需要常规 fragment 。您可以使用 SupportFragmentTestUtil.startFragment(fragment, activityClass)

支持库允许在尚不支持 fragment 的 API 级别上使用 fragment 。如果您的应用程序需要在 API 11 及更高版本 (Android 3) 上运行 - 您可以直接从 android.app.Fragment 派生您的 fragment

关于android - 使用 Robolectric 3.0 测试 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38219861/

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