我尝试使用Robolectric
@RunWith(RobolectricTestRunner.class)
@Config(
manifest = "...mypath.../AndroidManifest.xml",
qualifiers = "fr-normal-port-hdpi",
sdk = 21,
application = Application.class)
public class SetPaymentActivityTest {
@Before
public void setUp() throws Exception {
}
@Test
public void sendWrongPaymentDetails() throws Exception {
SetPaymentActivity activity = Robolectric.buildActivity(SetPaymentActivity.class)
// .attach()
.create()
// .starst()
// .resume()
// .visible()
.get();
activity = Robolectric.setupActivity(SetPaymentActivity.class);
// Shadows.shadowOf(SetPaymentActivity.class);
String url = activity.getUrl();
assertThat(url, equalTo(""));
}
}
PROD代码:
public class SetPaymentActivity extends FragmentActivity implements DaggerActivity {
但是我收到了这个错误:
Time: 109.712
There was 1 failure:
1) sendWrongPaymentDetails(com.google.android.apps.ridematch.payments.SetPaymentActivityTest)
java.lang.ClassCastException: android.app.Application cannot be cast to com.google.android.apps.common.inject.InjectedApplication
at com.google.android.apps.common.inject.ActivityInjectHelper.initGraph(ActivityInjectHelper.java:56)
我做错了什么?
答案 0 :(得分:0)
也许您需要SetPaymentActivityTest
扩展InjectedApplication
public class SetPaymentActivityTest extends InjectedApplication {
答案 1 :(得分:0)
很容易修复。
将测试代码更改为下一个:
@RunWith(RobolectricGradleTestRunner.class)
@Config(
constants = BuildConfig.class,
qualifiers = "fr-normal-port-hdpi",
sdk = 21)
问题是Robolectric
尝试使用您在配置中指定的Application
,并且在某些时候将其投射到您的班级
答案 2 :(得分:0)
我创建了一个虚拟的TestApplication类:
public TestApplication extends InjectedApplication
并配置:
@Config(
manifest = "...mypath.../AndroidManifest.xml",
qualifiers = "fr-normal-port-hdpi",
sdk = 21,
application = TestApplication.class)