2016-06-30 15 views
1

Ich erstelle einen Junit-Test, um zu prüfen, ob die richtige Aktivität per Knopfdruck gestartet wird.RuntimeException mit Roboelectric

@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); 

    } 
} 

Grundsätzlich vergleiche ich die aktuelle Aktivität die Absicht, die die nächste auflöst. Allerdings bekomme ich eine wirklich seltsame Ausnahme.

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) 

Mein MainActivity Code ist

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)); 
     } 
    }); 
} 
} 

Wie kann ich dieses Problem beheben?

Danke.

+0

entfernen Manifest Teil von '@ Config' –

+0

Eigentlich hatte ich diese @RunWith (RobolectricGradleTestRunner.class) statt @RunWith (RobolectricTestRunner.class) hinzuzufügen. – Theo

+1

Richtig, diesen Teil verpasst! Erfolg –

Antwort

0

Eigentlich hatte ich diese

@RunWith (RobolectricGradleTestRunner.class)

statt

@RunWith (RobolectricTestRunner.class) hinzuzufügen.

+0

RobolectricGradleTestRunner ist jetzt veraltet –

Verwandte Themen