You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This code allows to embed own implementations of classes into mockable android JAR. In my case I embed: Log & SparseArray classes.
What script do:
register patchMockableAndroidJar task as finalizer of the mockableAndroidJar
patch all testing tasks classpath - include our version of mockable android instead of original
copy test resources into folder where unit tests can find them during execution
three tasks needed for that: copyMockableAndroidJar, patchMockableAndroidJar, copy${flavor}${buildType}TestResources
/* [ UNIT TESTING ] ================================================================================================= */
afterEvaluate {
/* Adjust Mockable Android task */
tasks.matching { it.name.startsWith('mockableAndroidJar') }.each { mock->// http://goo.gl/wXhBIF --> MockableJarGenerator.java
mock.finalizedBy patchMockableAndroidJar
}
/* Patch classpath of the test tasks for using our own version of the mockable JAR */
tasks.matching { it.name.startsWith("test") }.each { t->def old ="mockable-android-${androidTargetSdkVersion}.jar"def mock ="${project.buildDir}\\intermediates\\patched-mockable-android-${androidTargetSdkVersion}.jar"try {
t.classpath = files(t.classpath.files, mock).filter { it.name != old }
} catch (finalThrowable ignored) {
// ignore task
}
}
/* Include resources. See: https://code.google.com/p/android/issues/detail?id=64887#c13 */
project.android.productFlavors.each { f->
project.android.buildTypes.each { b->/** compose dynamic tasks */def tName ="copy${f.name.capitalize()}${b.name.capitalize()}TestResources"
project.task(tName, type: Copy) {
description ="Make resources available for test: test${f.name.capitalize()}${b.name.capitalize()}."
group ='Unit Testing'
from project.android.sourceSets["test"].resources.srcDirs
into "${project.buildDir}/intermediates/classes/test/${f.name}/${b.name}"
}
tasks.matching { it.name.equalsIgnoreCase("test${f.name}${b.name}") }.each {
it.dependsOn tName
}
}
}
}
/* Create a copy of original mockable Jar */
task copyMockableAndroidJar(type: Copy) {
description ='Create backup copy of mockable-android-${api}.jar file'
group ='Unit Testing'// incremental build support
inputs.property "api", androidTargetSdkVersion
outputs.files "mockable-android-${androidTargetSdkVersion}-original.jar"
from "${project.buildDir}/intermediates/mockable-android-${androidTargetSdkVersion}.jar"
into "${project.buildDir}/intermediates/"
rename { StringfileName->
fileName.replace("mockable-android-${androidTargetSdkVersion}.jar",
"mockable-android-${androidTargetSdkVersion}-original.jar")
}
}
/* Exclude classes that we want to replace by own implementation */
task patchMockableAndroidJar(type: Zip, dependsOn: copyMockableAndroidJar) {
description ='patch mockable-android-${api}.jar by own classes'
group ='Unit Testing'// set archive name and destination
entryCompression =ZipEntryCompression.STORED// speedup task by excluding compression
destinationDir = file("${project.buildDir}/intermediates")
archiveName ="patched-mockable-android-${androidTargetSdkVersion}.jar"def source ="${project.buildDir}/intermediates/mockable-android-${androidTargetSdkVersion}-original.jar"// exclude from Mocks Jar classes which we replace by own implementation
from(zipTree(source)) {
exclude '**/android/util/SparseArray.class'
exclude '**/android/util/Log.class'
}
}
The text was updated successfully, but these errors were encountered:
OleksandrKucherenko
changed the title
Enable Mocked classes for Unit Tests
[Contribute] Enable Mocked classes for Unit Tests
Jun 1, 2015
This code allows to embed own implementations of classes into mockable android JAR. In my case I embed: Log & SparseArray classes.
What script do:
patchMockableAndroidJar
task as finalizer of themockableAndroidJar
copyMockableAndroidJar
,patchMockableAndroidJar
,copy${flavor}${buildType}TestResources
The text was updated successfully, but these errors were encountered: