0

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 protected]>",
        "email.example.com",
        "email@[email protected]",
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "あいうえお@example.com",
        "[email protected] (Joe Smith)",
        "email@example",
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "[email protected]"
    )

    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

1 Answer 1

0

Finally found the problem, I need to add the runner dependency androidx.test:runner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.