Last active
August 29, 2015 14:23
-
-
Save up1/2b7ec75397e81855bee2 to your computer and use it in GitHub Desktop.
Building Effective Unit Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
androidTestCompile 'com.android.support.test:runner:0.3' | |
androidTestCompile 'com.android.support.test:rules:0.3' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(AndroidJUnit4.class) | |
public class ReadDataFromContext { | |
@Test | |
public void readAppNameFromStringResource() { | |
ReadData readData = new ReadData(InstrumentationRegistry.getTargetContext()); | |
assertEquals("GDG2015", readData.getAppName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
defaultConfig { | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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