2016-06-24 28 views
8

Ho appena iniziato a imparare un framework di test Mockito, ho seguito esercitazione ufficiale da: developer.android.comMockito su Android, Context.getString (id) e NullPointerException

Il codice è:

private static final String FAKE_STRING = "HELLO WORLD"; 

@Mock 
Context mMockContext; 

@Test 
public void readStringFromContext_LocalizedString() { 
    // Given a mocked Context injected into the object under test... 
    when(mMockContext.getString(R.string.hello_world)) 
      .thenReturn(FAKE_STRING); 
    ClassUnderTest myObjectUnderTest = new ClassUnderTest(mMockContext); 

    // ...when the string is returned from the object under test... 
    String result = myObjectUnderTest.getHelloWorldString(); 

    // ...then the result should be the expected one. 
    assertThat(result, is(FAKE_STRING)); 
} 

ho scritto segue ClassUnderTest:

public class ClassUnderTest { 

private Context context; 

public ClassUnderTest(Context context) 
{ 
    this.context=context; 
} 

public String getHelloWorldString() 
{ 
    return context.getString(R.string.hello_world); 
} 

}

un d dopo l'esecuzione sto ricevendo NullPointerException:

java.lang.NullPointerException 
at android.content.Context.getHelloWorldString(Context.java:293) 
at com.darekk.draggablelistview.unit_tests.MockitoTest.readStringFromContext_LocalizedString(MockitoTest.java:46) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
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.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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) 
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 
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:59) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619) 

Quando sostituisco Android Context con la mia classe funziona.

Piattaforma: IntelliJ IDEA Community Edition 15.0.6

apprezzo davvero alcun aiuto.

risposta

5

Questo codice dovrebbe essere la prima linea del test:

doReturn("Sample Hello world string") 
      .when(mMockContext) 
      .getString(any(Integer.class)); 

È necessario uno stubbing di un metodo

+1

Hmm, dovrei metterlo dentro il metodo getHelloWorldString()? dove esattamente ?, È strano. Pensavo che solo Context.getString (...) dovesse essere stubato. –

+0

Modificato la mia risposta. Non c'è bisogno di modificare la classe sotto test. –

+1

Ho questo: when (mMockContext.getString (R.string.hello_world)) .thenReturn (FAKE_STRING); –

1

Sembra che si sta utilizzando il codice esatto da developer.android.com. A quanto pare hanno pubblicato un esempio incompleta .. Aggiungi questo metodo per il test:

@Before 
public void setup(){ 
    MockitoAnnotations.initMocks(this); 
} 

Per ulteriori riferimenti cassa Mockito's docs