使用mockito模拟android模式

vwoqyblh  于 2023-01-19  发布在  Android
关注(0)|答案(4)|浏览(111)

我想用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())总是返回false,这很令人困惑。

inb24sb2

inb24sb21#

在这种特殊情况下,不必使用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开始

d5vmydt9

d5vmydt92#

您正在执行电子邮件字段的验证
你不是在嘲笑这里返回真或假的行为。2还要注意我们不能嘲笑final类(模式)。
当regex模式与值匹配时,它返回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:模拟Matcher行为以返回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);


            }

    }
ghhaqwfi

ghhaqwfi3#

在这种情况下,使用Robolectric可以保存很多麻烦。

bvhaajcl

bvhaajcl4#

您可以使用Mockito 2模拟final类https://github.com/mockito/mockito/wiki/What 's-new-in-Mockito-2#模拟不可模拟的选择模拟final类方法

相关问题