Android单元测试抛空指针异常
Android unit test throw null pointer exception
我正在尝试测试有效性函数。我的功能是这样的:
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.")
}
}
}
如果用户凭据出现任何问题,我会使用此函数进行抛出。否则,什么也不会发生,代码继续。异常在其他应用层处理。
这是我的测试用例:
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 = "test@application.com", 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 = "test@application.com", 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 = "test@application.com",
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.")
}
}
}
问题是:
只有第一个测试用例通过,它检查电子邮件不为空。其他测试用例因 java.lang.NullPointerException
错误而失败。
有什么问题?
尝试使用 PatternsCompat.EMAIL_ADDRESS
而不是 Patterns.EMAIL_ADDRESS
我正在尝试测试有效性函数。我的功能是这样的:
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.")
}
}
}
如果用户凭据出现任何问题,我会使用此函数进行抛出。否则,什么也不会发生,代码继续。异常在其他应用层处理。 这是我的测试用例:
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 = "test@application.com", 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 = "test@application.com", 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 = "test@application.com",
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.")
}
}
}
问题是:
只有第一个测试用例通过,它检查电子邮件不为空。其他测试用例因 java.lang.NullPointerException
错误而失败。
有什么问题?
尝试使用 PatternsCompat.EMAIL_ADDRESS
而不是 Patterns.EMAIL_ADDRESS