有谁知道如何使用Robolectric测试以下设置?
包含ViewPager的片段,使用CursorLoader加载的数据.
使用下面的代码,CursorLoader永远不会被推入视图寻呼机的适配器中.我被await()电话困住了.
EventsFragmentTest.java:
@RunWith(CustomRobolectricTestRunner.class)
public class EventsFragmentTest extends AbstractDbAndUiDriver
{
// which element in the view pager we are testing
private static final int TEST_INDEX = 0;
protected SherlockFragmentActivity mActivity;
protected EventsFragment_ mFragment;
@Override
@Before
public void setUp() throws Exception
{
// create activity to hold the fragment
this.mActivity = CustomRobolectricTestRunner.getActivity();
// create and start the fragment
this.mFragment = new EventsFragment_();
}
@Test
public void sanityTest()
{
// create an event
final Event event = this.createEvent();
// …Run Code Online (Sandbox Code Playgroud) 我是测试世界的新手,更是Android测试世界的新手.在对Robolectric进行研究时,有助于对android进行测试的一件事让我感到困惑.有时在网络上我看到人们testCompile在引用Robolectric而其他人使用时在gradle构建脚本的依赖关系中使用关键字androidTestCompile.当然两者都无效?
有人可以解释两者之间的区别,以及使用Robolectric时应该使用哪一个?
android gradle robolectric android-testing android-gradle-plugin
我正在尝试使用Robolectric 2.1.1运行单元测试,我无法让它膨胀自定义布局(例如ViewPagerIndicator类).假设这是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"
android:id="@+id/test_test"/>
<com.viewpagerindicator.CirclePageIndicator
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
考虑一下我的测试类:
@RunWith(RobolectricTestRunner.class)
public class TestRoboActivityTest {
private TestRoboActivity mActivity;
@Before
public void setUp() throws Exception {
mActivity = Robolectric.buildActivity(TestRoboActivity.class).create().get();
}
@After
public void tearDown() throws Exception {
mActivity = null;
}
@Test
public void testSanity() throws Exception {
Assert.assertNotNull(mActivity);
}
}
Run Code Online (Sandbox Code Playgroud)
执行'mvn clean test'会导致
Tests in error: testSanity(TestRoboActivityTest): XML file .\res\layout\test.xml line #-1 (sorry, not yet implemented): Error inflating class com.viewpagerindicator.CirclePageIndicator
很酷,所以似乎还不支持自定义视图.在他们的网站上检查示例Robolectric项目,一个解决方案可能是从LayoutInflater扩展布局: …
我有一些测试,我想用Robolectric运行,我使用2.3-SNAPSHOT因为我的APP使用ActionbarCompat我需要使用2.3-SNAPSHOT版本,因为Robolectric以前找不到AppCompat主题.所以我在Eclipse中设置了Classpath,我最终得到了这个:
java.lang.UnsupportedOperationException: Robolectric does not support API level 9, sorry!
at org.robolectric.SdkConfig.<init>(SdkConfig.java:24)
at org.robolectric.RobolectricTestRunner.pickSdkVersion(RobolectricTestRunner.java:288)
at org.robolectric.RobolectricTestRunner.getEnvironment(RobolectricTestRunner.java:264)
at org.robolectric.RobolectricTestRunner.access$100(RobolectricTestRunner.java:57)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:186)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:172)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Run Code Online (Sandbox Code Playgroud)
我的测试项目的清单是这样的:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vendor.test"
android:versionCode="1"
android:versionName="1.0">
<application>
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.vendor" />
</manifest>
Run Code Online (Sandbox Code Playgroud)
无论我使用什么,我总是抱怨API等级.
有人这个有用吗?
我知道有一个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);
Run Code Online (Sandbox Code Playgroud)
我正在使用API级别13(Honeycomb).
谢谢.
我有一个与远程android服务交互的小应用程序.我想在单元测试中模拟该服务.我使用Robolectric和JUnit其他测试用例和阴影,但我无法想象如何处理远程服务.
是否足以使用相同的包创建和启动测试服务,使用相同的实际服务和导出方法aidl?
由于我没有该服务的代码,我认为我不能使用需要实际类Robolectric的ShadowService.
非常感谢.
我可以测试一下retrofit2beta4的真实反应吗?我需要Mockito还是Robolectic?
我的项目中没有活动,它将是一个库,我需要测试服务器是否正确响应.现在我有这样的代码而且卡住了......
@Mock
ApiManager apiManager;
@Captor
private ArgumentCaptor<ApiCallback<Void>> cb;
@Before
public void setUp() throws Exception {
apiManager = ApiManager.getInstance();
MockitoAnnotations.initMocks(this);
}
@Test
public void test_login() {
Mockito.verify(apiManager)
.loginUser(Mockito.eq(login), Mockito.eq(pass), cb.capture());
// cb.getValue();
// assertEquals(cb.getValue().isError(), false);
}
Run Code Online (Sandbox Code Playgroud)
我可以做出虚假的回应,但我需要测试真实.它成功了吗?它的身体是否正确?你能帮我代码吗?
刚刚开始使用Robolectric,它似乎是我所需要的.但是,我在使用SharedPreferences方面遇到了一些障碍.
我有两个测试用例
Activity需要一个新的/空的sharedPreferences
Activity期望sharedPreferences中包含一些数据
对于测试用例1,测试按预期传递,所以很好:)
但是,对于测试案例2,我似乎无法找到一个很好的方法来为Robolectric提供一些虚假数据,因此Activity能够访问这些假数据.
感觉就像一个非常常见的用例,但我似乎无法弄清楚如何做到这一点!
想知道哪个是为Android应用程序和库编写单元测试用例的更好选择:使用Robolectric库或坚持使用Android测试框架.我想在命令行运行测试套件,并希望它独立于配置模拟器或让设备连接到构建机器的需要.你们有没有人对这两者或其他更好的事情进行比较分析?您的经验将很有助于我决定更好的解决方案.
junit continuous-integration android unit-testing robolectric