Issue
I'm trying to create a new Matcher for espresso in order to be able to select a list item. This works fine for a simple class like Office. See this example.
private fun withOffice(title: String): Matcher<Any> {
return object : BoundedMatcher<Any, Office>(Office::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("with title '$title'");
}
public override fun matchesSafely(office: Office): Boolean {
return office.name == title
}
}
}
However things get more difficult when bringing in generics, like in this class.
class KeyTranslationPair<F, S> extends Pair<F, S>
Trying to create a Matcher like this
private fun withCompanyType(companyType: CompanyType): Matcher<Any> {
return object : BoundedMatcher<Any, KeyTranslationPair<CompanyType, String>>(KeyTranslationPair<CompanyType, String>::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("with companyType '$companyType'");
}
public override fun matchesSafely(keyTranslationPair: KeyTranslationPair<CompanyType, String>): Boolean {
return keyTranslationPair.key == companyType
}
}
}
results in the following error
My assumption is that kotlin get things mixed up with the java type system. Maybe someone has an idea here.
Solution
That's because KeyTranslationPair<CompanyType,Strnig> is not a class, when is saying class means KeyTranslationPair::class.java, so, you can do it like :
return object : BoundedMatcher<Any, KeyTranslationPair<*,*>>(KeyTranslationPair::class.java)
And you are saying that you don't know what's inside of the KeyTranslationPair, and since it's a Generic you'll have to change the matchesSafely to :
override fun matchesSafely(item: KeyTranslationPair<*, *>?): Boolean {
return item?.key == companyType
}
And also you can check if Key is an instance of CompanyType doing :
override fun matchesSafely(item: KeyTranslationPair<*, *>?): Boolean {
if(item?.key is CompanyType){
return item.key == companyType
}
return false
}
Hope it helps.
Answered By - Skizo-ozᴉʞS

0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.