我创建了一个junit测试,以检查是否通过单击按钮启动了正确的活动。
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class,
manifest="src/main/AndroidManifest.xml", sdk = 21)
public class MainActivityTest {
MainActivity activity;
@Before
public void setUp(){
activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.login).performClick();
}
@Test
public void clickingLogin_shouldStartLoginActivity() {
Intent expectedIntent = new Intent(activity, LoginActivity.class);
assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
}
}
基本上,我将当前活动与下一个活动的意图进行比较。但是,我得到了一个非常奇怪的例外。
java.lang.RuntimeException: Could not find any resource from reference ResName{testing.theo.robotutorial:style/Theme_AppCompat_Light_DarkActionBar} from style StyleData{name='AppTheme', parent='Theme_AppCompat_Light_DarkActionBar'} with theme null
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getParent(ShadowAssetManager.java:520)
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getAttrValue(ShadowAssetManager.java:458)
at org.robolectric.shadows.ShadowResources.getOverlayedThemeValue(ShadowResources.java:296)
at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:285)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:190)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:55)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:485)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:480)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:475)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.app.Activity.onApplyThemeResource(Activity.java:3673)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:140)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:85)
at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:90)
at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:85)
at org.robolectric.shadows.CoreShadowsAdapter$1.setThemeFromManifest(CoreShadowsAdapter.java:35)
at org.robolectric.util.ActivityController.attach(ActivityController.java:58)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at org.robolectric.util.ActivityController.setup(ActivityController.java:210)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
at testing.theo.robotutorial.MainActivityTest.setUp(MainActivityTest.java:27)
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:251)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
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:152)
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)
我的MainActivity代码是
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View button = findViewById(R.id.login);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
});
}
}
如何解决此问题?
感谢。
答案 0 :(得分:0)
其实我不得不添加这个
@RunWith(RobolectricGradleTestRunner.class)
而不是
@RunWith(RobolectricTestRunner.class)。