Issue
I have a Fragment implementation which I want to test using Robolectric framework. When I try to run the test it fails with exception:
java.lang.NullPointerException
at org.robolectric.shadows.ShadowViewGroup$ShadowLayoutParams.__constructor__(ShadowViewGroup.java:141)
at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java)
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java)
at com.lalala.ui.FullPickerFragment.onCreateView(FullPickerFragment.java:41)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:29)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
at org.robolectric.util.Scheduler.postAtFrontOfQueue(Scheduler.java:98)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:114)
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:1519)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at com.lalala.ui.FullPickerFragmentTest.init(FullPickerFragmentTest.java:137)
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.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:245)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
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:149)
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)
It fails in test case method marked with @Before annotation on this line:
activity.getSupportFragmentManager()
.beginTransaction()
.add(pickerFragment, TAG)
.commit();
FullPicketFragment's code it refers to is place in onCreateView method and here it is:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(LOG_TAG, "onCreateView()");
fragmentView = inflater.inflate(R.layout.bf_full_picker, container, false);
mFragmentViewContainer = new FrameLayout(getActivity());
ViewGroup.LayoutParams fragmentViewParams = fragmentView.getLayoutParams();
FrameLayout.LayoutParams fragmentViewContainerParams =
new FrameLayout.LayoutParams(fragmentViewParams);
mFragmentViewContainer.setLayoutParams(fragmentViewContainerParams);
mFragmentViewContainer.addView(fragmentView);
return mFragmentViewContainer;
}
As you can see I simply inflate layout and place it into FrameLayout with LayoutParams property copied from inflated one. Robolectric fails to create that LayoutParams object.
How can I fix this?
I tried to create a copy of LayoutParams using another constructor and it didn't help. Also the code itself working fine the issues is only in test case intialization. Tests were working fine until I made those changes in onCreateView.
I'm using Robolectric 3.0-rc3.
Solution
It seems that fragmentView.getLayoutParams() returns null and line after it is put into constructor. You can try this:
public View onCreateView(...) {
mFragmentViewContainer = new FrameLayout(inflater.getContext());
fragmentView = inflater.inflate(
R.layout.bf_full_picker,
mFragmentViewContainer,
**true**);
return mFragmentViewContainer;
}
Answered By - FeelGood
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.