Skip to content

Instantly share code, notes, and snippets.

@up1
Last active August 29, 2015 14:23
Show Gist options
  • Save up1/2b7ec75397e81855bee2 to your computer and use it in GitHub Desktop.
Save up1/2b7ec75397e81855bee2 to your computer and use it in GitHub Desktop.
Building Effective Unit Tests
java.lang.RuntimeException: Method getString in android.content.Context not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details.
at android.content.Context.getString(Context.java)
dependencies {
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}
@RunWith(MockitoJUnitRunner.class)
public class MockExampleTest {
@Mock
Context mockContext;
@Test
public void readStringFromContext() {
when(mockContext.getString(R.string.app_name)).thenReturn("GDG2015");
ReadData readdata = new ReadData(mockContext);
String actualResult = readdata.getAppName();
assertEquals("GDG2015", actualResult);
}
}
@RunWith(AndroidJUnit4.class)
public class ReadDataFromContext {
@Test
public void readAppNameFromStringResource() {
ReadData readData = new ReadData(InstrumentationRegistry.getTargetContext());
assertEquals("GDG2015", readData.getAppName());
}
}
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment