如何在Android项目中运行新风格的MockApplication?
我的应用有两种风格。为了进行浓缩咖啡测试,我创建了另一种味道,名为如何在Android项目中运行新风格的MockApplication?,android,android-espresso,Android,Android Espresso,我的应用有两种风格。为了进行浓缩咖啡测试,我创建了另一种味道,名为mock,以模拟服务器响应,并创建我想要测试的东西 因此,我的应用程序项目的build.gradle文件如下所示: android { useLibrary 'org.apache.http.legacy' sourceSets { main { res.srcDirs = [ 'src/main/res', 'src/main/r
mock
,以模拟服务器响应,并创建我想要测试的东西
因此,我的应用程序项目的build.gradle
文件如下所示:
android {
useLibrary 'org.apache.http.legacy'
sourceSets {
main {
res.srcDirs = [
'src/main/res', 'src/main/res-flags', ...
]
}
test {
res.srcDirs = ['src/test/resources']
}
}
defaultConfig {
applicationId "com.my.package_1"
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
productFlavors {
mock {
applicationId "com.mock.package"
...
}
flavor_1 {
applicationId "com.my.package_1"
...
}
flavor_2 {
applicationId "com.my.package_2"
...
}
}
buildTypes {
debug {
// Setup default urls
buildConfigField "String", "API_URL_BASE", "\"https://www.example_1.com\""
// Enabling multidex support.
multiDexEnabled true
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
}
release {
// Enable when testing proguard
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.release
// Setup default urls
buildConfigField "String", "API_URL_BASE", "\"https://www.example_2.com\""
}
applicationVariants.all { variant ->
variant.outputs.each { v ->
def f = v.outputFile
def sha = 'git rev-parse --short HEAD'.execute().text.trim()
def fname = f.name.replace(".apk", "-${defaultConfig.versionName}-${defaultConfig.versionCode}-${sha}.apk")
v.outputFile = new File(f.parent, fname)
}
}
}
// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
if(variant.buildType.name.equals('release')
&& variant.getFlavors().get(0).name.equals('mock')) {
variant.setIgnore(true);
}
}
// Always show the result of every unit test, even if it passes.
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
}
我创建了一个模拟文件夹,它有自己的manifest.xml
文件,看起来如下:
android {
useLibrary 'org.apache.http.legacy'
sourceSets {
main {
res.srcDirs = [
'src/main/res', 'src/main/res-flags', ...
]
}
test {
res.srcDirs = ['src/test/resources']
}
}
defaultConfig {
applicationId "com.my.package_1"
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
productFlavors {
mock {
applicationId "com.mock.package"
...
}
flavor_1 {
applicationId "com.my.package_1"
...
}
flavor_2 {
applicationId "com.my.package_2"
...
}
}
buildTypes {
debug {
// Setup default urls
buildConfigField "String", "API_URL_BASE", "\"https://www.example_1.com\""
// Enabling multidex support.
multiDexEnabled true
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
}
release {
// Enable when testing proguard
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.release
// Setup default urls
buildConfigField "String", "API_URL_BASE", "\"https://www.example_2.com\""
}
applicationVariants.all { variant ->
variant.outputs.each { v ->
def f = v.outputFile
def sha = 'git rev-parse --short HEAD'.execute().text.trim()
def fname = f.name.replace(".apk", "-${defaultConfig.versionName}-${defaultConfig.versionCode}-${sha}.apk")
v.outputFile = new File(f.parent, fname)
}
}
}
// Remove mockRelease as it's not needed.
android.variantFilter { variant ->
if(variant.buildType.name.equals('release')
&& variant.getFlavors().get(0).name.equals('mock')) {
variant.setIgnore(true);
}
}
// Always show the result of every unit test, even if it passes.
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
}
我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<permission
android:name="com.mock.passenger.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.mock.passenger.permission.MAPS_RECEIVE"/>
<!-- Settings for GCM -->
<permission
android:name="com.mock.passenger.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.mock.passenger.gcm.permission.C2D_MESSAGE"/>
<application
android:name="com.mock.passenger.MockApplication"
tools:replace="android:name">
...
</application>
</manifest>
您可以使用自定义测试运行程序加载
模拟应用程序
。不需要口味
public class MockTestRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(
ClassLoader cl, String className, Context context)
throws InstantiationException,
IllegalAccessException,
ClassNotFoundException {
return super.newApplication(cl, MockApplication.class.getName(), context);
}
}
然后,在build.gradle
文件中使用它:
testInstrumentationRunner 'com.example.MockUnitRunner'
更多信息: