我如何用Robolectrictesting片段?
我知道有一个Robolectric.shadowOf(Fragment)方法和一个ShadowFragment类,认为它们没有列在文档上,但是我不能使它工作。 
 myFragment = new MyFragment(); myFragment.onCreateView(LayoutInflater.from(activity), (ViewGroup) activity.findViewById(R.id.container), null); myFragment.onAttach(activity); myFragment.onActivityCreated(null); 
我正在使用API级别13(Honeycomb)。
谢谢。
编辑#4和#5 :在Robolectric 3. *中 ,他们分割了片段起始函数。
 对于支持片段,您将需要添加依赖到您的build.gradle : 
 testCompile "org.robolectric:shadows-support-v4:3.3.2" 
 导入: org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment; 
 对于平台片段,您不需要此依赖关系。 导入: import static org.robolectric.util.FragmentTestUtil.startFragment; 
 它们都使用相同的名称startFragment() 。 
 import static org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment; @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment fragment = YourFragment.newInstance(); startFragment( fragment ); assertNotNull( fragment ); } } 
  编辑#3 :Robolectric 2.4有一个支持和定期碎片的API 。 您可以使用newInstance()模式,也可以在构buildFragment时使用构造函数。 
 import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.assertNotNull; import static org.robolectric.util.FragmentTestUtil.startFragment; @RunWith(RobolectricGradleTestRunner.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment fragment = new YourFragment(); startFragment( fragment ); assertNotNull( fragment ); } } 
编辑#2 :如果您使用支持片段( 支持常规活动/片段的应该在下一个版本中 ) ,则有一个新的帮助器:
 import static org.robolectric.util.FragmentTestUtil.startFragment; @Before public void setUp() throws Exception { fragment = YourFragment.newInstance(); startFragment( fragment ); } 
编辑 :如果你升级到Robolectric 2.0:
 public static void startFragment( Fragment fragment ) { FragmentActivity activity = Robolectric.buildActivity( FragmentActivity.class ) .create() .start() .resume() .get(); FragmentManager fragmentManager = activity.getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add( fragment, null ); fragmentTransaction.commit(); } 
原始答案
正如其他评论者所build议的那样,您确实需要使用片段pipe理器(而不是调用上面列出的生命周期方法)。
 @RunWith(MyTestRunner.class) public class YourFragmentTest { @Test public void shouldNotBeNull() throws Exception { YourFragment yourFragment = new YourFragment(); startFragment( yourFragment ); assertNotNull( yourFragment ); } 
我创build了一个testing运行器,并有一个为我启动一个片段的function,所以我可以在任何地方使用它。
 public class MyTestRunner extends RobolectricTestRunner { public MyTestRunner( Class<?> testClass ) throws InitializationError { super( testClass ); } public static void startFragment( Fragment fragment ) { FragmentManager fragmentManager = new FragmentActivity().getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add( fragment, null ); fragmentTransaction.commit(); } } 
你们都是这么做的。 只要使用FragmentTestUtil。
 FragmentTestUtil.startFragment(yourfragment); 
支持片段已被移至模块:
阴影支持-V4
(截至2015年7月,Robolectric v3.0)
添加gradle依赖到app / build.gradle:
 testCompile 'org.robolectric:shadows-support-v4:3.0' 
然后导入到你的Robolectrictestingjava类:
 import org.robolectric.shadows.support.v4.SupportFragmentTestUtil; 
然后,您可以开始使用support-v4片段进行testing:
 @Test public void minimalFragmentTest() throws Exception { MyFunFragment fragment = new MyFunFragment(); SupportFragmentTestUtil.startVisibleFragment(fragment); assertThat(fragment.getView()).isNotNull(); } 
参考文献:
- github更新日志 ,移动支持片段到不同的模块
 我敢肯定你必须使用FragmentManager创build一个FragmentManager ,那么它将工作。 
我只是想在Robolectric 2.0中补充一点:
 activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get(); fragment.show(activity.getSupportFragmentManager(), null); fragment.getDialog(); //This stills returns null 
 它仍然为我返回null。 我做的是添加activity.getSupportFragmentManager().executePendingTransaction(); 它的工作。 
似乎robolectric不运行这个由于某种原因。 似乎Looper可能暂停了一些东西。 任何方式这为我工作,它看起来像这样:
 activity = Robolectric.buildActivity(FragmentActivity.class).create().start().resume().get(); fragment.show(activity.getSupportFragmentManager(), null); activity.getSupportFragmentManager().executePendingTransactions(); fragment.getDialog();