Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.util.Patterns Unit test returns NullPointerException [duplicate]

I'm starting to work with unit tests in android I have trying to test a method that uses:

android.util.Patterns.EMAIL_ADDRESS.matcher(validEmail).matches()

it should return true as I added this in my build.gradle

testOptions { 
    unitTests {
        returnDefaultValues = true
    }
} 

Still the test throws a NullPointerException.

I have two questions: 1- How do I fix this? 2- Should I reconsider my design and remove the android dependency to a mock object.

// @ LoginPresenterTest
@Test
public void clickOnLogin_loginSuccess(){
    loginPresenter.login(validEmail, validPassword);
    verify(loginView).setLoginButton(false);
}
// LoginPresenter
public void login(String email, String password) {

    loginView.setLoginButton(false);
    if(!isValid(email, password)){
        loginView.setLoginButton(true);
        return;
    }
}
// Validation
public static boolean isEmailValid(String email){
    return !(email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches());
}

When android.utills is commented their will be no exception.

like image 570
Abdelrhman Talat Avatar asked Aug 01 '16 07:08

Abdelrhman Talat


1 Answers

static methods are not mocked. You need to use some mocking framework like mockito or powermock.

like image 156
Nayan Srivastava Avatar answered Oct 21 '22 06:10

Nayan Srivastava