Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock android Patterns with mockito

I want validate an email with some code provided by Android.

Here is the code I want to mock :

 if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
      throw new InvalidPhoneException(phone);

In my test file :

@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);
    }

I got this error when I launch the tests :

java.lang.NullPointerException
    at ch.mycompany.myapp.model.UserTest.mockValidator(UserTest.java:59)

EDIT 1 :

I tried :

    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);

But when I do that, my code (Patterns.EMAIL_ADDRESS.matcher(email).matches()) return always false. This is confusing.

like image 748
Xero Avatar asked Dec 14 '22 01:12

Xero


2 Answers

In this particular case it is not necessary to mock the Patterns class with mockito (it is unmockable anyways), just change your source code as follows:

 if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
  throw new InvalidPhoneException(phone);

should be

 if(!PatternsCompat.EMAIL_ADDRESS.matcher(email).matches())
  throw new InvalidPhoneException(phone);

That did the job for me.

like image 160
Manuel Mariano Silva Avatar answered Dec 26 '22 06:12

Manuel Mariano Silva


you are performing the validation of the email field

you are not mocking the behaviour here when to return true or false. Also make a note we cannot mock final classes (Pattern).

when regex pattern matches with the value it returns true or false

Instead of complicating the things . Simply perform the valiation by passing the value.

Solution 1 :

     @Test
        public void test() throws Exception{            
            String email="[email protected]";         
            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

Solution 2: Mocking the Matcher behaviour to return true or false

 @RunWith(PowerMockRunner.class)
    @PrepareForTest({Pattern.class,Matcher.class})
    public class TestEmailPattern {


            @Test
            public void test() throws Exception{            
                String email="[email protected]";         
                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);



            }


    }
like image 38
Barath Avatar answered Dec 26 '22 05:12

Barath