1、PowerMock和Robolectric教程
- Robolectric教程:http://robolectric.org/
- Powermock教程:https://github.com/powermock/powermock
2、Robolectric中context 如何获取
用Robolectric中的RuntimeEnvironment.application
参考资料:
3、解决Robolectric中的Resources$NotFoundException问题(context无法拿到resource文件)
Gridle中加入以下代码:
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
gradle.properties中加入:
android.enableUnitTestBinaryResources=true
参考资料:
4、Powermock和Robolectric如何一起使用
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
注意:如果是4.0+的Robolectric,constants不需要再配置
5、Method put in org.json.JSONObject not mocked
testOptions {
unitTests {
includeAndroidResources = true
unitTests.returnDefaultValues = true
}
}
参考资料:http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-
6、not prepared for test:
明明已经在@PrepareForTest中加入了该类还是报这个错误,解决:
检查是否加入了:
@Rule
public PowerMockRule rule = new PowerMockRule();