mardi 31 mai 2016

Android Studio. Two similar tests, yet one passes the other fails

I'm new to Android studio and Java in general.

Im currently doing a test class and these two code are very similar(only change in layout variable) but 2 different results. Using Roboelectric.

This one passes

/*Test to verify next button */
@Test
public void checkNextButton(){
    /* Simulate button press */
    activity.findViewById(R.id.nextButton).performClick();

    /*Capture view*/
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    View view = shadowActivity.findViewById(android.R.id.content);

    /*Check view is not null*/
    assertNotNull(view);
}

This one fails.

/* Test to verify back button */
@Test
public void checkBackButton() {
    /* Simulate button press */
    activity.findViewById(R.id.backButton).performClick();

    /*Capture view*/
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    View view = shadowActivity.findViewById(android.R.id.content);

    /*Check view is not null*/
    assertNotNull(view);

}

The only difference is in the following line,

activity.findViewById(R.id.****Button).performClick();

I've double check the button Id and both only change the screen to the needed layout. I cannot think of a valid why one would pass while the other fails.

The console shows the following,

java.lang.NullPointerException at com.presentech.handsup.AudienceTutorialTest.checkBackButton(AudienceTutorialTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 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.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Aucun commentaire:

Enregistrer un commentaire