Run instrumented test inside module's androidTest

39 Views Asked by At

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

There are 1 best solutions below

0
Bitwise DEVS On BEST ANSWER

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