Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Contribute] Enable Mocked classes for Unit Tests #12

Closed
OleksandrKucherenko opened this issue Jun 1, 2015 · 0 comments
Closed

[Contribute] Enable Mocked classes for Unit Tests #12

OleksandrKucherenko opened this issue Jun 1, 2015 · 0 comments

Comments

@OleksandrKucherenko
Copy link

This code allows to embed own implementations of classes into mockable android JAR. In my case I embed: Log & SparseArray classes.

What script do:

  1. register patchMockableAndroidJar task as finalizer of the mockableAndroidJar
  2. patch all testing tasks classpath - include our version of mockable android instead of original
  3. copy test resources into folder where unit tests can find them during execution
  4. 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 (final Throwable 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 { String fileName ->
    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'
  }
}
@OleksandrKucherenko OleksandrKucherenko changed the title Enable Mocked classes for Unit Tests [Contribute] Enable Mocked classes for Unit Tests Jun 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant