Android Studio 3.3
В приложении / build.gradle:
ext.ESPRESSO_VERSION = '3.2.0-alpha02'
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'
testImplementation 'junit:junit:4.12'
}
Эспрессо-тест:
@Test
fun toolbar_title_textColor2() {
onView(withId(R.id.toolbaTitleTextView))
.check(matches(withItemText(containsString("Color.WHITE"))))
}
пользовательский сопоставитель:
fun withItemText(matcherText: Matcher<String>): Matcher<Any> {
// use preconditions to fail fast when a test is creating an invalid matcher.
checkNotNull(matcherText)
return object : TypeSafeMatcher<Any>() {
override fun describeTo(description: Description) {
description.appendText("expected text: $matcherText")
}
public override fun describeMismatchSafely(
item: Any,
mismatchDescription: Description) {
mismatchDescription.appendText("actual text: $item")
}
public override fun matchesSafely(item: Any): Boolean {
return matcherText == item
}
}
}
Здесь сообщение об ошибке:
Started running tests
androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'expected text: a string containing "Color.WHITE"' doesn't match the selected view.
Expected: expected text: a string containing "Color.WHITE"
Got: "AppCompatTextView{id=2131231010, res-name=toolbaTitleTextView, visibility=VISIBLE, width=1080, height=168, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@95b2081, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Traders, input-type=0, ime-target=false, has-links=false}"
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
Как видите, напечатайте текст ожидаемый текст:
Но почему бы не напечатать текст: Фактический текст: ?