2016-07-09 2 views
2

Я понятия не имею, почему «намереваясь» в следующем коде возвращает null. Я просто скопировал код здесь: https://google.github.io/android-testing-support-library/docs/espresso/intents/index.htmlНамеренный возврат null (или имеет нулевой аргумент)?

Исключение составляет

java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.test.espresso.intent.OngoingStubbing android.support.test.espresso.intent.Intents.internalIntending(org.hamcrest.Matcher)' on a null object reference 
at android.support.test.espresso.intent.Intents.intending(Intents.java:155) 
at GsonActivityTest.ensureHandleActivityResultCorrectly(GsonActivityTest.java:37) 
at java.lang.reflect.Method.invoke(Native Method) 
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 android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) 
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
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.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
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.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879) 

Код

import android.app.Activity; 
import android.app.Instrumentation; 
import android.content.Intent; 
import android.support.test.rule.ActivityTestRule; 
import android.support.test.runner.AndroidJUnit4; 

import com.example.myapplication.R; 
import com.example.myapplication.activity.GsonActivity; 

import org.junit.Rule; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
import static android.support.test.espresso.assertion.ViewAssertions.matches; 
import static android.support.test.espresso.intent.Intents.intending; 
import static android.support.test.espresso.intent.matcher.IntentMatchers.toPackage; 
import static android.support.test.espresso.matcher.ViewMatchers.withId; 
import static android.support.test.espresso.matcher.ViewMatchers.withText; 

@RunWith(AndroidJUnit4.class) 
public class GsonActivityTest { 

@Rule 
public ActivityTestRule<GsonActivity> rule = new ActivityTestRule<>(GsonActivity.class); 

@Test 
public void ensureHandleActivityResultCorrectly() { 
    // Build a result to return when a particular activity is launched. 
    Intent resultData = new Intent(); 
    String phoneNumber = "123-345-6789"; 
    resultData.putExtra("phone", phoneNumber); 
    Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData); 

    // Set up result stubbing when an intent sent to "contacts" is seen. 
    intending(toPackage("com.android.contacts")).respondWith(result); 

    // User action that results in "contacts" activity being launched. 
    // Launching activity expects phoneNumber to be returned and displays it on the screen. 
    onView(withId(R.id.button8)).perform(click()); 

    // Assert that data we set up above is shown. 
    onView(withId(R.id.editText)).check(matches(withText(phoneNumber))); 
} 
} 
+0

Возможный дубликат [Сбой теста на эспрессо] (http: // stacko verflow.com/questions/37869418/espresso-intent-test-failing) – cammando

ответ

5

Вы должны использовать IntentsTestRule вместо ActivityTestRule

+1

Спасибо. Я немного недоволен тем, что Google указывает это в одной строке текстов вместо этого в своем примерном коде. Легко пропустить. –

Смежные вопросы