0

I'm trying to verify that my code that interacts with a WebView control is properly passing Javascript calls into the WebView, but my unit tests are not executing correctly. It looks like maybe the tests are running against the wrong version of Android.

My test:

@Test
public void testJavascript() {
  // myObj will pass its argument as a string to a WebView control referenced in the
  // test as mWebView
  myObj.callJavascript("some code"); // passes to a WebView control

  // Using Mockito to verify calls to mocked WebView
  verify(mWebView).evaluateJavascript(anyString(), any(ValueCallback.class));
}

I'm building with a target of Android 4.4, using Gradle for my build system. I'm new to Gradle and may be screwing something up. As far as I can tell, Maven Central does not have Android 4.4 up, so I'm using the local .jar file from my Android Studio install.

from build.gradle:

android {
  compileSdkVersion 19
  defaultConfig {
    minSdkVersion 9
    targetSdkVersion 19
  }
}

sourceSets {
  unitTest {
    java.srcDir file('src/test/java')
    resources.srcDir file('src/test/res')
  }
}

configurations {
  unitTestCompile.extendsFrom runTime
  unitTestRuntime.extendsFrom unitTestCompile
}

dependencies {
  unitTestCompile files("$project.buildDir/classes/release")
  unitTestCompile 'junit:junit:4.11'
  unitTestCompile 'org.robolectric:robolectric:2.2'
  unitTestCompile 'org.mockito:mockito-core:1.9.5'
  unitTestCompile files('../libs/android-19.jar') // From Android Studio
}

task unitTest(type:Test, dependsOn:assemble) {
  testClassesDir = project.sourceSets.unitTest.output.classesDir
  classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn unitTest

tasks.withType(Test) {
  scanForTestClasses = false
  include "**/*Tests.class"
}

When I try to run my test via gradle unitTest I get an error that I can't make sense of:

com.example.Tests > testCallJavascript FAILED
java.lang.NoSuchMethodError: android.webkit.WebView.evaluateJavascript(Ljava/lang/String;Landroid/webkit/ValueCallback;)V
    at com.exampleTests.testCallJavascript(Tests.java:...)

UPDATE:

Okay, after adding a debug println to check the version of the SDK that the tests are running against, it looks like they're running against APILEVEL 14, which does not support WebView.evaluateJavascript(). As far as I can tell, I have never configured this project to target APILEVEL 14. And ideas where I should be looking to figure out what is configured wrong? Thanks!

Greg
  • 10,360
  • 6
  • 44
  • 67

3 Answers3

1

Robolectric only support one version of Android AFAIK, as it has to fake a whole lot of Android stuff.

So if you run your tests in robolectric you are stuck with what version it emulates. To control the environment, run your test in an emulator of your choice.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • From the Robolectric people I've learned that it falls back to API level 14 if it finds a version of Android that's too high for it to handle. They say they should have a version that can work with Android 4.4 available in the next few weeks. – Greg Dec 12 '13 at 14:52
1

API-19 is not yet supported by Robolectric. In case an invalid API-target is used, Robolectric defaults to API-14.

See: https://groups.google.com/forum/#!msg/robolectric/LBOgSkD5O9U/qooiA_xDv_4J

Padi
  • 291
  • 3
  • 4
0

As per the Gradle Android plugin docs, the API level to compile/run against in the build is configured as follows:

android {
    compileSdkVersion 17
}
Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • I've updated my question to show (relevant parts of?) the android configuration section from my build.gradle. I'm targeting API level 19, and the tests are building correctly against API level 19. They're being run against API level 14 though, and that's what I'm trying to figure out. – Greg Dec 11 '13 at 15:24