我想用Android提供的一些代码验证电子邮件。
这是我想模拟的代码:
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
throw new InvalidPhoneException(phone);
在我的测试文件中:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Patterns.class })
public class UserTest {
@Before
public void mockValidator() {
mockStatic(Patterns.class);
when(Patterns.EMAIL_ADDRESS.matcher(any(String.class)).matches()).thenReturn(true);
}
启动测试时出现此错误:
java.lang.NullPointerException
at ch.mycompany.myapp.model.UserTest.mockValidator(UserTest.java:59)
编辑 1 :
我试过了:
mockStatic(Patterns.class);
Field field = PowerMockito.field(Patterns.class, "EMAIL_ADDRESS");
field.set(Patterns.class, mock(Pattern.class));
// prepare matcher
Matcher matcher = mock(Matcher.class);
when(matcher.matches())
.thenReturn(true);
// final mock
when(Patterns.EMAIL_ADDRESS.matcher(any(String.class)))
.thenReturn(matcher);
但是当我这样做时,我的代码(Patterns.EMAIL_ADDRESS.matcher(email).matches())返回总是假的。这是令人困惑的。
在这种特殊情况下,没有必要用 mockito 模拟 Patterns 类(无论如何它是不可模拟的),只需按如下方式更改源代码:
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
throw new InvalidPhoneException(phone);
应该是
if(!PatternsCompat.EMAIL_ADDRESS.matcher(email).matches())
throw new InvalidPhoneException(phone);
这为我做了工作。
将Patterns
替换为PatternsCompat
从androidx.core.util.PatternsCompat
您正在执行电子邮件字段的验证
你不是在嘲笑这里的行为,什么时候返回真或假。还要注意我们不能模拟期末课程(模式)。
当正则表达式模式与值匹配时,它返回 true 或 false
而不是使事情复杂化.只需通过传递值来执行评估。
解决方案 1 :
@Test
public void test() throws Exception{
String email="hello@gmail.com";
System.out.println(Patterns.EMAIL_ADDRESS.matcher(email).matches());
}
protected static class Patterns{
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
private static final Pattern EMAIL_ADDRESS=Pattern.compile(EMAIL_PATTERN);
}
output :
true
解决方案 2:模拟匹配器行为以返回 true 或 false
@RunWith(PowerMockRunner.class)
@PrepareForTest({Pattern.class,Matcher.class})
public class TestEmailPattern {
@Test
public void test() throws Exception{
String email="hello@gmail.com";
Pattern pattern=PowerMockito.mock(Pattern.class);
Matcher matcher=PowerMockito.mock(Matcher.class);
PowerMockito.when(matcher.matches()).thenReturn(true);
assertEquals(Patterns.EMAIL_ADDRESS.matcher(email).matches(),true);
}
protected static class Patterns{
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$";
private static final Pattern EMAIL_ADDRESS=Pattern.compile(EMAIL_PATTERN);
}
}
在这种情况下,使用 Robolectric 会为您省去很多麻烦。
您可以使用 Mockito 2 模拟期末课程https://github.com/mockito/mockito/wiki/What's-new-in-Mockito-2#mock-the-unmockable-opt-in-mocking-of-final-classesmethods