Is it possible to run an instrumented test in an Android module that has no activity but uses AppCompat dependencies? I am attempting to run a simple test but only works if moved to app module.
Util class to test
/**
* Typical email validation
* */
fun validateEmail(email: String?): ValidationResult {
val emailInput: String? = email?.trim()
return when {
(emailInput.isNullOrBlank()) -> {
ValidationResult(
isSuccessful = false,
errorMessageResource = R.string.field_required
)
}
(Patterns.EMAIL_ADDRESS.matcher(emailInput).matches().not()) -> {
ValidationResult(
isSuccessful = false,
errorMessageResource = R.string.invalid_email
)
}
else -> {
ValidationResult(
isSuccessful = true
)
}
}
}
Test script
@RunWith(AndroidJUnit4::class)
class ValidationUtilsTest {
@Test
fun invalidEmails_returnsFalse() {
val invalidEmailList = listOf(
"plainaddress",
"#@%^%#$@#$@#.com",
"@example.com",
"Joe Smith <email@example.com>",
"email.example.com",
"email@example@example.com",
".email@example.com",
"email.@example.com",
"email..email@example.com",
"あいうえお@example.com",
"email@example.com (Joe Smith)",
"email@example",
"email@-example.com",
"email@example.web",
"email@111.222.333.44444",
"email@example..com",
"Abc..123@example.com"
)
val listResult = mutableListOf<Boolean>()
for (invalidEmail in invalidEmailList) {
val result = ValidationUtils.validateEmail(invalidEmail)
listResult.add(result.isSuccessful)
}
assertThat(listResult.any { true }).isFalse()
}
}
Getting these error:
java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.test.runner.AndroidJUnitRunner" on path: DexPathList
Structure
- app
- libs
- androidTest
- test
Finally found the problem, I need to add the runner dependency androidx.test:runner
.