Android Studio 3.2 。 我想在Espresso的测试中使用 WireMock 。
在build.gradle中
buildscript {
ext.KOTLIN_VERSION = '1.3.21'
ext.ESPRESSO_VERSION = '3.2.0-alpha02'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
在app / build.gradle
中android {
dataBinding {
enabled = true
}
compileSdkVersion 28
defaultConfig {
applicationId "com.myproject"
minSdkVersion 18
targetSdkVersion 28
versionCode 6
versionName "0.0.7"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation('com.crashlytics.sdk.android:crashlytics:2.7.0@aar') { transitive = true; }
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha03'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha03'
implementation 'com.android.support:design:28.0.0'
implementation 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.yuyh.json:jsonviewer:1.0.6'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION"
implementation project(':common')
androidTestImplementation "androidx.test.espresso:espresso-core:$ESPRESSO_VERSION"
androidTestImplementation "androidx.test.espresso:espresso-intents:$ESPRESSO_VERSION"
androidTestImplementation "androidx.test.espresso:espresso-contrib:$ESPRESSO_VERSION"
androidTestImplementation 'androidx.test:rules:1.1.2-alpha02'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'
androidTestImplementation("com.github.tomakehurst:wiremock:2.18.0") {
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'asm', module: 'asm'
exclude group: 'org.json', module: 'json'
exclude group: 'com.google.guava', module:'guava'
}
testImplementation 'junit:junit:4.12'
}
这是我的意式浓缩咖啡的测试:
import android.content.Intent
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import android.graphics.Color
import android.graphics.Typeface
import androidx.test.espresso.action.ViewActions.*
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import com.github.tomakehurst.wiremock.junit.WireMockRule
@RunWith(AndroidJUnit4::class)
class AddTraderActivityTest {
@get:Rule
var wireMockRule = WireMockRule(wireMockConfig().port(8081))
@Test
fun toolbarTitleTextViewText() {
onView(withId(R.id.toolbaTitleTextView)).check(matches(withText(R.string.add_trader)))
}
}
但是我得到了错误:
$ adb shell am instrument -w -r -e debug false -e class 'com/myproject.android.activity.AddTraderActivityTest#toolbarTitleTextViewText' com/myproject.android.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/io/Resources;
at com.github.tomakehurst.wiremock.core.WireMockConfiguration.<init>(WireMockConfiguration.java:58)
at com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig(WireMockConfiguration.java:104)
at com/myproject.android.activity.AddTraderActivityTest.<init>(AddTraderActivityTest.kt:55)
at java.lang.reflect.Constructor.newInstance(Native Method)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:389)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.io.Resources" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com/myproject.android.debug.test-2/base.apk", zip file "/data/app/com/myproject.android.debug-2/base.apk"],nativeLibraryDirectories=[/data/app/com/myproject.android.debug.test-2/lib/arm, /data/app/com/myproject.android.debug-2/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 30 more
Suppressed: java.lang.ClassNotFoundException: com.google.common.io.Resources
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 31 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
答案 0 :(得分:2)
对我有用的是删除排除项:
exclude group: 'com.google.guava', module:'guava'
来自 app/build.gradle 中的 wiremock 依赖项实现。
我在谷歌上搜索了 com.google.common.io.Resources
的来源,它是番石榴,所以如果您不包括来自另一个项目包含的番石榴,您必须删除排除项。
或者,我相信你也可以添加
implementation "com.google.guava:guava:30.1-android"
到您的 Gradle 文件,它也应该可以工作(排除仍然存在)。
答案 1 :(得分:1)
在 Espresso/Instrumented 测试中明确使用 Wiremock,但从单元测试中排除(使用 Kotlin DSL)
androidTestApi("com.github.tomakehurst:wiremock:2.18.0") {
exclude("org.apache.httpcomponents", "httpclient")
exclude("org.ow2.asm", "asm")
exclude("org.json", "json")
}
androidTestApi("org.apache.httpcomponents:httpclient-android:4.3.5.1")