Hem*_*S R 5 java junit android junit4 android-studio
我正在编写一个单元测试来检查电子邮件验证逻辑.运行测试时,逻辑是抛出空指针异常.但它适用于模拟器.有人可以帮我解决这个问题吗?
public static String validate(String email, String password) {
if (email == null || email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
return "Enter valid email address";
}
if (password.isEmpty() || password.length() < 4) {
return "Password should be between 4 and 10 alphanumeric characters";
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
以下是我的单元测试.
@Test
public void validateShouldReturnMessageIfYouPassAnInvalidEmail() throws Exception {
String validateMessage = LoginService.validate("abcdef", "password");
assertEquals("Enter valid email address", validateMessage);
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是,
java.lang.NullPointerException
at com.tryout.hemanth.expensetracker.service.LoginService.validate(LoginService.java:11)
Run Code Online (Sandbox Code Playgroud)
android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()
自 API 级别 8 起就存在,可能您的模拟器早于此。
所以我建议EMAIL_ADDRESS_PATTERN
按照此答案中的建议添加自定义字符串并进行检查
if (email == null || email.isEmpty() || !EMAIL_ADDRESS_PATTERN.matcher(email).matches()){
return "Enter valid email address";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1214 次 |
最近记录: |