Home > other >  Android unit test throw null pointer exception
Android unit test throw null pointer exception

Time:02-26

I'm trying to test a validity function. My function is like this:


class InvalidCredentialException(message: String) : Exception(message)


@Throws
fun credentialValidityChecker(email: String, password: String, nameAndFamily: String? = null) {
    when {
        email.isBlank() -> {
            throw InvalidCredentialException("Email address can't left blank.")
        }
        !Patterns.EMAIL_ADDRESS.matcher(email)
            .matches() -> {
            throw InvalidCredentialException("Email address format is not correct.")
        }
        password.isBlank() -> {
            throw InvalidCredentialException("Password can't left blank.")
        }
        password.length < 5 -> {
            throw InvalidCredentialException("Password should have at least 5 characters.")
        }
        nameAndFamily != null -> {
            if (nameAndFamily.isBlank())
                throw InvalidCredentialException("Name and family can't left blank.")
        }
    }
}


I use this function to throw in case of any problem with the user credential. Otherwise, nothing happens, and the code continues. The exception is handled in other application layers. and here are my test cases:


class CredentialValidityTest {

    @Test
    fun emptyEmail_raiseEmptyEmailException() {
        try {
            credentialValidityChecker(email = "", password = "12345")
            fail("Empty email should raise exception.")
        } catch (e: InvalidCredentialException) {
            assertThat(e.message).isEqualTo("Email address can't left blank.")
        }
    }

    @Test
    fun wrongFormatEmail_raiseWrongEmailException() {
        val wrongFormatEmailList = listOf(
            "test", "test@", "test@application",
            "test@application.", "test@.", "test.application@com"
        )
        for (email in wrongFormatEmailList)
            try {
                credentialValidityChecker(email = email, password = "12345")
                fail("Wrong format email should raise exception.")
            } catch (e: InvalidCredentialException) {
                assertThat(e.message).isEqualTo("Email address format is not correct.")
            }
    }

    @Test
    fun emptyPassword_raiseEmptyPasswordException() {
        try {
            credentialValidityChecker(email = "[email protected]", password = "")
            fail("Empty password should raise exception.")
        } catch (e: InvalidCredentialException) {
            assertThat(e.message).isEqualTo("Password can't left blank.")
        }
    }

    @Test
    fun weakPassword_raiseWeakPasswordException() {
        try {
            credentialValidityChecker(email = "[email protected]", password = "1234")
            fail("weak password should raise exception.")
        } catch (e: InvalidCredentialException) {
            assertThat(e.message).isEqualTo("Password should have at least 5 characters.")
        }
    }

    @Test
    fun emptyNameAndFamily_raiseEmptyNameAndFamilyException() {
        try {
            credentialValidityChecker(
                email = "[email protected]",
                password = "12345",
                nameAndFamily = ""
            )
            fail("Empty name and family should raise exception.")
        } catch (e: InvalidCredentialException) {
            assertThat(e.message).isEqualTo("Name and family can't left blank.")
        }
    }

}

The problem is:

Only the first test case pass, which checks email not be blank. The other test cases fail with the java.lang.NullPointerException error. What is the problem?

CodePudding user response:

Try using PatternsCompat.EMAIL_ADDRESS instead of Patterns.EMAIL_ADDRESS

  • Related