vendredi 27 mai 2016

Mocking a context fails on my computer only

We have just started implementing unit tests for our project in Android Studio, and I have this problem bugging me for 2 days now. One of my colleagues has written a small unit test example that sees if Context.getString() returns a specific string. The test class body is as follows:

private static final String FAKE_STRING = "some_string";

@Mock
Context context;

@Test
public void readStringFromContext_LocalizedString() {
    // we specify that when the "getString" method of the mocked context is called, we want it to return the FAKE_STRING
    when(context.getString(R.string.app_name)).thenReturn(FAKE_STRING);
    ClassUnderTest myObjectUnderTest = new ClassUnderTest(context);
    String result = myObjectUnderTest.getStringToTest();
    assertThat(result, is(FAKE_STRING));
}

When run on their computers, the test passes, there are no issues. But, when I try to run it, I get the exception:

java.lang.RuntimeException: Stub!

at android.content.Context.getString(Context.java:14)
at examples.MockingObjectsExampleTest.readStringFromContext_LocalizedString(MockingObjectsExampleTest.java:32)
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.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.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)

The error points to the line:

when(context.getString(R.string.app_name)).thenReturn(FAKE_STRING);

I have tried all I could find online, and to no avail. Also, I am baffled that another coleague had some other issues with the unit tests, upgraded his Android Studio to the latest version (2.1.1, which I already have), "did something" and now it works for him. Of course, he can't remember what he did. Is it some other file / configuration that I should change? I already have the build.gradle dependencies in place, like everyone else. Thank you.

Aucun commentaire:

Enregistrer un commentaire