robolectric

Android Testing with Robolectric and Dagger

半腔热情 提交于 2019-12-04 05:49:14
问题 I am trying to write an Android application using Dagger. Trying to follow the TDD approach, I started writing a test for my First activity. For writing tests I am using Robolectric and I am trying to make it work in different scenarios using Mockito. Short story: I have an android activity which I want to test using robolectric. This activity has some of its dependencies provided through Dagger. I managed to make this work by overriding the Application class and providing a mock of the

getSupportActionBar() returns null with Robolectric

こ雲淡風輕ζ 提交于 2019-12-04 05:06:18
The method getSupportActionBar() returns null when i invoke it through a Test Case based in Roboelectric and JUnit. This is my simple test case: package com.mobile.test; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import android.app.Activity; import android.content.Intent; import com.mobile.android.core.R; import com.mobile.android.core.activity.MainActivity; import

Testing a CursorLoader with Robolectric & Mockito

懵懂的女人 提交于 2019-12-04 04:57:38
Given I'm developing a simple ListFragment (in this case, it reads a list of Artists from the MediaStore, but will also read data from a different source later) like this: @EFragment public class ArtistsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { private static final String TAG = ArtistsFragment.class.getName(); private SimpleCursorAdapter mAdapter; Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI; CursorLoader mCursorLoader; @AfterViews void setupView() { mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, null,

Robolectric+Eclipse Can't find resources?

ε祈祈猫儿з 提交于 2019-12-04 02:39:12
I just configured a test project for my Android app to use Robolectric. I followed the Eclipse Quick Start . An exception is raised executing my simple very first test. java.lang.RuntimeException: java.lang.NullPointerException at com.xtremelabs.robolectric.res.ResourceLoader.init(ResourceLoader.java:93) at com.xtremelabs.robolectric.res.ResourceLoader.getStringValue(ResourceLoader.java:271) at com.xtremelabs.robolectric.shadows.ShadowResources.getString(ShadowResources.java:56) at android.content.res.Resources.getString(Resources.java) at org.xxx.mobile.android.teldir.app

Assert ImageView was loaded with specific drawable resource ID

喜欢而已 提交于 2019-12-04 02:10:32
I'm writing a Robolectric unit test and I need to make an assertion that an ImageView had setImageResource(int) called on it with a certain resource ID. I'm using fest-android for assertions but it doesn't appear to contain this assertion. I also tried to get the ShadowImageView from Robolectric for the ImageView because I know it used to give you access to this, but it's now gone. Lastly, I tried to call setImageDrawable in my code instead of setImageResource, then in my test assert like this: assertThat(imageView).hasDrawable(resources.getDrawable(R.drawable.some_drawable)); but this also

How to run unit tests with dependency to an Android library module?

落爺英雄遲暮 提交于 2019-12-04 01:48:52
问题 Whenever I try to run unit-tests for classes in my app module that depend on classes from an library module, I get this: java.lang.NoClassDefFoundError: de/ivu/junittest/DummyData at de.ivu.junittest.app.DummyModel.<init>(DummyModel.java:16) at DummyModelTest.testInstantiation(DummyModelTest.java:7) ... In the above sample, DummyData is part of the lib module, while DummyModel is part of the app module. DummyModel has a member of type DummyData , but instantiating this in the test-class

Android: Unit testing Android applications with Robolectric and Mockito

自古美人都是妖i 提交于 2019-12-04 00:27:51
I have a Java library that uses a few things from the Android APIs. I'd like to use Mockito to write unit tests for this library. Is there a way I can go about this? Mockito doesn't play nice on the Dalvik VM, see this post: Using Mockito with Android virtual machine UPDATE: Since this post I've discovered Robolectric, and I've had the opportunity to work out of Pivotal Labs and make some small contributions to this library. I would recommend using this over the Android testing framework/mockito. Also, you're free to use Robolectric AND Mockito, but the shadow objects in Robolectric make

How do you force a configuration change in an Android Robolectric test?

蓝咒 提交于 2019-12-03 22:29:08
I'm using robolectric to make my android unit tests fast enough to be useful. I want to test that code I've written works while the screen orientation is changing to simulate a common real world use case. Specifically what I'm testing is an asynchronous http call to a server with some xml parsed after the result is fetched. I have the unit test for all that working great but can't figure out how to simulate the screen rotation. Any state change that causes the activity to recreate itself is fine, it doesn't necessarily have to be screen rotation. A solution that uses the emulator is not an

Use in process-build disappeared in Android Studio v1.3

前提是你 提交于 2019-12-03 17:09:47
I have recently updated my Android Studio to 1.3 in Beta channel, and I noticed that Use in process-build has been disappeared from Compiler -> Gradle settings menu. Then I wonder how can I debug my Robolectric test considering a known issue as explained in How to debug when running Robolectric tests in Android Studio? and http://www.culmination.org/2014/03/debugging-during-robolectric-test-execution-in-android-studio/ I receive java.net.SocketException "Socket closed" error and I do not know how to disable Use in process-build . Issue tracking My team has also been experiencing this issue

Android单元测试(七):Robolectric,在JVM上调用安卓的类

风流意气都作罢 提交于 2019-12-03 15:47:59
今天讲讲Android上做单元测试的最后一个难点,那就是在JVM上无法调用安卓相关的类,不然的话,会报类似于下的错误: java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked. 关于这个话题,其实我以前是写过的,也许今天我回过头来写这个话题,会采用不一样的形式,不一样的心态来写,然而,作为我写过的第一篇关于单元测试的文章,而且看看时间,是去年的6月15号,再过几天,刚好一周年。想想这篇文章是在我刚开始探索,尝试在安卓上面写单元测试的时候,写的一篇文章,如今因为安卓单元测试的原因,我认识了很多同行,甚至不时有人叫我“大牛大神”之类的,虽然知道大家是客气,我也受之有愧,但怎么滴心里也有点虚荣的开心,哈哈哈。。因此现在回过头去看看当时自己写的东西,不禁觉得有点那啥。。。因此,我决定把之前的文章稍作补充和修改,作为这个系列的第七篇。 ----------------------以下文字写于去年今天----------------------- 作为一只本科非计算机专业的程序猿,手动写单元测试是我从来没接触过的东西,甚至在几个月前,我都不知道单元测试是什么东西。倒不是说没听过这个词,也不是不知道它的大概是什么东西——“用来测试一个方法,或者是一小块代码的测试代码”。然而真正是怎么做的